I need to write a script that takes in variables and makes a share on a remote system.This works:Invoke-Command -ComputerName server -ScriptBlock {$a = [WMICLASS]"Win32_Share"; $a.Create("C:\test","test",0)}But this doesn't:$sharepath = "C:\test"$sharename = "test"Invoke-Command -ComputerName server -ScriptBlock {$a = [WMICLASS]"Win32_Share"; $a.Create($sharepath,$sharename,0)}I need a way to pass those values somehow. 解决方案 The remote session can't read your local variables, so you need to send them with your command. There's a few options here. In PowerShell 2.0 you could:1.Pass them along with -ArgumentList and use $arg[i]$sharepath = "C:\test"$sharename = "test"Invoke-Command -ComputerName server -ScriptBlock {$a = [WMICLASS]"Win32_Share"; $a.Create($args[0],$args[1],0)} -ArgumentList $sharepath, $sharename2.Pass them along with -ArgumentList and use param() in your scriptblock to define the arguments$sharepath = "C:\test"$sharename = "test"Invoke-Command -ComputerName server -ScriptBlock { param($sharepath, $sharename) $a = [WMICLASS]"Win32_Share"; $a.Create($sharepath,$sharename,0)} -ArgumentList $sharepath, $sharenameIn PowerShell 3.0, the using-variable scope was introduced to make it easier:$sharepath = "C:\test"$sharename = "test"Invoke-Command -ComputerName server -ScriptBlock { $a = [WMICLASS]"Win32_Share"; $a.Create($using:sharepath,$using:sharename,0)}You could read more about this on about_Remote_Variables @ TechNet 这篇关于在远程命令中使用局部变量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-18 13:04