[英]Powershell PSSession Not Working
Trying to test PSSession with powershell 尝试使用Powershell测试PSSession
Enter-PSSession -ComputerName <servername>
Invoke-Command -ComputerName <servername> -ScripBlock {
Get-PSDrive -PSProvider FileSystem | %{ write-host 'Checking drive ' $_.Root; set-location $_.Root; Get-ChildItem -Filter "load4.wav" -Recurse; write-host 'Checked drive ' $_.Root; } | Out-File "$env:UserProfile\Desktop\$env:ComputerName-Results.txt"
}
I would eventually like to be able to run this against a list of servers and save save the results locally if possible 我最终希望能够针对服务器列表运行此命令,并在可能的情况下将结果保存到本地
So when I run it will enter the PSSession 所以当我运行它会进入PSSession
[server]: PS C:\Users\US_Chris.Ondrovic\Documents>
But will not execute this part of the code 但是不会执行这部分代码
Invoke-Command -ComputerName server -ScripBlock {
Get-PSDrive -PSProvider FileSystem | %{ write-host 'Checking drive ' $_.Root; set-location $_.Root; Get-ChildItem -Filter "load4.wav" -Recurse; write-host 'Checked drive ' $_.Root; } | Out-File "$env:UserProfile\Desktop\$env:ComputerName-Results.txt"
}
So I can verify this works like it is supposed to on the remote machine 所以我可以验证它是否可以在远程计算机上正常运行
Invoke-Command -ComputerName <server> -ScriptBlock {Get-Service} | Out-File $env:Userprofile\Desktop\Test.txt
Got it some what working, now it runs on through the PSSession but isn't looking for what I need 得到了一些有效的方法,现在它可以通过PSSession运行,但是并没有寻找我需要的东西
function Remote-Search() {
param([string[]]$Servers, [string]$Item)
foreach($server in $Servers) {
Invoke-Command -ComputerName $server -ScriptBlock {Get-PSDrive -PSProvider FileSystem | %{ write-host 'Checking drive ' $_.Root; set-location $_.Root; Get-ChildItem -Filter $item -Recurse; write-host 'Checked drive ' $_.Root; }} | Out-File $env:Userprofile\Desktop\$server-results-$item.txt
}
}
function Remote-Search() {
param([string[]]$Servers, [string]$Item)
foreach($server in $Servers) {
Invoke-Command -ComputerName $server -ArgumentList $Item -ScriptBlock { param([string]$filter)
Get-PSDrive -PSProvider FileSystem | %{
write-host 'Checking drive ' $_.Root
set-location $_.Root
Get-ChildItem -Filter $filter -Recurse
write-host 'Checked drive ' $_.Root
}
} | Out-File $env:Userprofile\Desktop\$server-results-$item.txt
}
}
Variables in your script aren't available directly to the script block. 脚本中的变量不能直接用于脚本块。 You can declare parameters in the script block and then pass values to them through the -ArgumentList
parameter of Invoke-Command
. 您可以在脚本块中声明参数,然后通过Invoke-Command
的-ArgumentList
参数将值传递给它们。
So here, I've added a $filter
parameter to the script block, and I passed in $Item
through the -ArgumentList
parameter of Invoke-Command
. 因此,在这里,我向脚本块添加了$filter
参数,并通过Invoke-Command
的-ArgumentList
参数传入了$Item
。 Now in the script block, on the remote machine, $filter
will have that value. 现在在远程计算机上的脚本块中, $filter
将具有该值。
Invoke-Command -ComputerName server -ScripBlock {
Get-PSDrive -PSProvider FileSystem | %{ write-host 'Checking drive ' $_.Root; set-location $_.Root; Get-ChildItem -Filter "load4.wav" -Recurse; write-host 'Checked drive ' $_.Root; } | Out-File "$env:UserProfile\Desktop\$env:ComputerName-Results.txt"
}
Assuming this was copied verbatim, you are missing a "t" in -ScriptBlock
假设逐字复制,则-ScriptBlock
中缺少“ t”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.