Powershell-一些命令赢了';t使用Invoke命令运行
我正在尝试从服务器向大约50个运行Powershell的客户端发送一些命令。大多数命令使用Invoke命令工作。我使用了与我的其他命令完全相同的格式,但是这个命令不起作用。基本上,我想让每个客户机从我的服务器获取一个.xml文件,以便以后导入它。我在这里的代码示例中缺少$credentials和其他变量,但它们在脚本的其他地方设置正确 在权限方面,winrm中的TrustedHosts设置为*并且脚本执行设置为无限制Powershell-一些命令赢了';t使用Invoke命令运行,powershell,Powershell,我正在尝试从服务器向大约50个运行Powershell的客户端发送一些命令。大多数命令使用Invoke命令工作。我使用了与我的其他命令完全相同的格式,但是这个命令不起作用。基本上,我想让每个客户机从我的服务器获取一个.xml文件,以便以后导入它。我在这里的代码示例中缺少$credentials和其他变量,但它们在脚本的其他地方设置正确 在权限方面,winrm中的TrustedHosts设置为*并且脚本执行设置为无限制 clear $temp = RetrieveStatu
clear
$temp = RetrieveStatus
$results = $temp.up #Contains pinged hosts that successfully replied.
$profileName = Read-Host "Enter the profile name(XML file must be present in c:\share\profiles\)"
$File = "c:\profiles\profile.xml"
$webclient = New-Object System.Net.WebClient
$webclient.Proxy = $NULL
$ftp = "ftp://anonymous:anonymous@192.168.2.200/profiles/$profileName"
$uri = New-Object System.Uri($ftp)
$command = {write-host (hostname) $webclient.DownloadFile($uri, $File)}
foreach($result in $results)
{
# download profile from C:\share\profiles
Invoke-Command $result.address -ScriptBlock $command -Credential $credentials
# add profile to wireless networks
# Invoke-Command $result.address -ScriptBlock {write-host (hostname) (netsh wlan add profile filename="c:\profiles\$args[0].xml")} -argumentlist $profileName -Credential $credentials
}
我得到以下错误:
You cannot call a method on a null-valued expression.
+ CategoryInfo : InvalidOperation: (DownloadFile:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
有什么想法吗?在本地运行时,同一命令在客户端上工作正常。在脚本块中使用
$webclient
,在脚本块中,$webclient
不会在另一端定义。为什么不在脚本块中创建web客户端,例如:
$command = {
param($profileName)
$File = "c:\profiles\profile.xml"
$webclient = New-Object System.Net.WebClient
$webclient.Proxy = $NULL
$ftp = "ftp://anonymous:anonymous@192.168.2.200/profiles/$profileName"
$uri = New-Object System.Uri($ftp)
Write-Host (hostname)
$webclient.DownloadFile($uri, $File)}
}
$profileName = Read-Host "Enter the profile name(XML file must be present in c:\share\profiles\)"
Invoke-Command $result.address -ScriptBlock $command -Credential $credentials -Arg $profileName
这将要求您通过
调用命令
上的-ArgumentList
参数将一些变量从客户端提供给远程机器。然后,这些提供的参数映射到scriptblock中的param()
语句。非常感谢!它工作得很好。我以为我可以在客户之外构建工人,但似乎不行。