Hi,
As $SQLInstance is defined outside of the scriptblock, you have to use the "using:" scope modifier to access the variable.
Invoke-Command -ComputerName $ServerName -ScriptBlock {Invoke-Sqlcmd -ServerInstance $using:SQLInstance -Query "EXEC sp_helpsrvrolemember 'sysadmin'"}
Or you can simply run Invoke-Sqlcmd without Invoke-Command
Invoke-Sqlcmd -ServerInstance $SQLInstance -Query "EXEC sp_helpsrvrolemember 'sysadmin'"
Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.