编写将在远程上下文中执行的脚本的一些最佳实践是什么?
例如,我刚刚发现$Profile远程执行期间内置的var 不存在.
Enter-PSSession在本地计算机上使用打开远程PowerShell会话时,如何在远程计算机上的配置文件中使用该功能.
我试图在远程Powershell 2.0会话中使用[Reflection.Assembly] :: LoadFrom加载.Net 4.0程序集.由于我对powershell.exe.config文件所做的更改,它在本地工作,但在远程会话中失败并显示"此程序集由运行时更新...构建"异常.
涉及的两台机器都有.Net 2.0和4.0,并且对x86和x64位powershell可执行文件进行了powershell.exe.config更改.我也尝试更改服务器powershell注册表项:HKLM:\ Software\Microsoft\Powershell\1\PowerShellEngine\RuntimeVersion HKLM:\ Software\Wow6432Node\Microsoft\Powershell\1\PowerShellEngine\RuntimeVersion
我一定错过了什么,但我不知道它是什么.
编辑: 以下是我正在执行的代码的示例.
PS C:\>Enter-PSSession -ComputerName server1
[server1]: PS C:\stuff> dir *.dll | foreach { [Reflection.Assembly]::LoadFrom( $_.FullName ) }
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用invoke-command远程执行powershell脚本.该脚本依赖于可通过本地网络获得的配置文件.脚本以下列方式调用:
Invoke-Command -ComputerName 192.168.137.181 -FilePath c:\scripts\script.ps1 -ArgumentList \\192.168.137.1\share\config.xml
Run Code Online (Sandbox Code Playgroud)
您可以看到的配置是一个xml文件,它使用以下命令加载:
$xml = New-Object XML
$xml.Load(args[0])
Run Code Online (Sandbox Code Playgroud)
当在机器上本地调用脚本时,它运行没有任何问题并读取配置文件.但是,当我使用invoke命令从不同的机器运行它时,我得到了
"访问路径'\\ 192.168.137.1\share\config.xml'被拒绝"
异常,在执行Load方法时抛出.
具有读写权限的每个人都可以访问该文件.应该运行脚本的机器(.181)和运行它的机器都具有相同的凭据,因此我不会在invoke-command cmdlet中传递它们.共享计算机(.1)具有不同的凭据,但从.181本地调用脚本时,这从来不是问题.
你能指点我正确的方向吗?我坚持这一步,自己无法找到解决方案.我尝试使用WebClient#DownloadString方法下载xml字符串并传递共享计算机的凭据,但它没有帮助.
提前致谢
我有两台服务器:
ServerA包含一个文件夹,其中包含需要从提升的PowerShell提示符执行的批处理文件(deploy.bat).在ServerA中,如果我从正常提示或PowerShell提示符运行它,它将失败.如果我从高架提示运行它可以工作.(以管理员身份运行).
我遇到的问题是当我尝试使用远程PowerShell执行从serverB执行批处理文件时.我能够使用此命令执行:
Invoke-Command -computername serverA .\remotedeploy.ps1
Run Code Online (Sandbox Code Playgroud)
remotedeploy.ps1的内容 是:
cd D:\Builds\build5
.\Deploy.bat
Run Code Online (Sandbox Code Playgroud)
我在stackoverflow中看了很多关于的问题:
这个问题同时涉及两个问题.所以确切的问题是:
可以在PowerShell中执行ELEVATED REMOTE脚本吗?
powershell powershell-2.0 elevated-privileges powershell-remoting
我见过很棒的文章
PowerShell 2.0远程指南:第8部分 - 远程处理方案和故障排除
http://www.ravichaganti.com/blog/?p=1181
我有这个问题:我在域中有两台计算机,在网络中.
第一次测试OK:从域中的计算机远程连接到域中的计算机
COMPANY_DOMAIN\desmonitor是域用户.
域名COMPANY_DOMAIN
计算机:iis01
PS C:\ Users\myUser> Enter-PSSession -Computername iisw01 -Credential COMPANY_DOMAIN\desmonitor
[desiisw01]:PS C:\ Users\desmonitor\Documents> exit
第二次测试错误::使用本地管理员用户从域中的计算机远程连接到域中的计算机
iis01\instalador是机器iis01
计算机的本地用户:iis01
PS C:\ Users\myUser> Enter-PSSession -Computername iis01 -Credential iis01\instalador Enter-PSSession:连接到远程服务器失败,并显示以下错误消息:WinRM客户端无法处理请求.
在使用COMPANY_DOMAIN\desmonitor的终端服务器(远程桌面)的机器ii01中,我打开PS控制台并执行
PS C:\ Users\desmonitor> winrm quickconfig
WinRM已设置为在此计算机上接收请求.
WinRM已经设置为在此计算机上进行远程管理.
PS C:\ Users\desmonitor>
然后,我再试一次,但我得到同样的错误:
PS C:\ Users\myUser> Enter-PSSession -Computername iis01 -Credential iis01\instalador Enter-PSSession:连接到远程服务器失败,并显示以下错误消息:WinRM客户端无法处理请求.
使用此命令:
PS C:\ Users\myUser> Set-Item WSMan:\ localhost\Client\TrustedHosts -Value"*" - Force PS C:\ Users\myUser>
现在,我可以访问:
PS C:\ Users\myUser> Enter-PSSession -Computername iis01 -Credential iis01\instalador [iiw01]:PS C:\ …
我正在尝试在3个终端服务器上找到其$ _.命令行属性中包含某些单词的进程.在我的域管理员帐户下,它工作正常.但是我希望这个脚本可以用于域用户,并且doamin用户在运行此脚本时会出错.
我应该怎么做,以便域用户可以像域管理员一样运行此脚本?提前致谢!
错误:
Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESS DENIED))
At N:\FindWhoIsUsing\FindWhoIsUsing.ps1:7 char:18
get-wmiobject <<<< win32_process -computername $server -EnableAllPrivileges|
CategoryInfo : NotSpecified: (:) [Get-WmiObject], UnauthorizedAccessException
FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
Run Code Online (Sandbox Code Playgroud)
Powershell代码:
Write-host "Who is using this profile?"
$profile = Read-host "specify profile name"
$servers = @("server-01","server-02","server-03")
Foreach($server in $servers)
{
Write-host $server
get-wmiobject win32_process -computername $server -EnableAllPrivileges|
where{$_.name -like "*Processname*" -and
$_.CommandLine -like "*$profile*"}|
select @{n="Server";e={$server}},@{n="User";e={$_.getowner().user}},@{n="ProcessID";e= {$_.ProcessID}},{$_.CommandLine}|fl
}
Write-host "DONE Searching!"
Run Code Online (Sandbox Code Playgroud) 对于遗留应用程序,我需要创建一个具有格式名称的注册表项c:/foo/bar/baz.(注意:正斜杠,而不是反斜杠.)要清楚:这是一个单词的名称,带有正斜杠,否则看起来像一个Windows路径.因为我需要在许多服务器上编写脚本,所以PowerShell似乎是一个很好的选择.
问题是我无法弄清楚如何通过PowerShell以该格式创建密钥. New-Item -Path HKLM:\SOFTWARE\Some\Key -Name 'c:/foo/bar/baz'PowerShell的错误认为我正在使用/路径分隔符并且无法找到路径HKLM:\Software\Some\Key\c:\foo\bar,这确实不存在(并且不应该).我找不到任何其他方式(ab)New-Item用来得到我想要的东西.
是否有我遗漏的东西,或者我应该放弃,只是以老式的方式生成和加载注册表转储?
我尝试使用以下代码从C#运行脚本localwindows.ps1:
PSCredential credential = new PSCredential(userName, securePassword);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "machineName", 5985, "/wsman", shellUri, credential);
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
runspace.Open();
String file = "C:\\localwindows.ps1";
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(file);
pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
}
Run Code Online (Sandbox Code Playgroud)
但是获得异常:'术语'C:\ localwindows.ps1'不被识别为cmdlet,函数,脚本文件或可操作程序的名称.
所以我尝试了以下方法:
PSCredential credential = new PSCredential(userName, securePassword);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "machineName", 5985, "/wsman", shellUri, credential);
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
runspace.Open();
using (PowerShell powershell = PowerShell.Create())
{
powershell.Runspace = runspace;
PSCommand new1 = new PSCommand();
String …Run Code Online (Sandbox Code Playgroud) 我有文件夹c:\ test我有三个文件:"file1","file2","file3"
以下脚本:
$remoteSession = New-PSSession -ComputerName localhost
$folder = "c:\test"
$exclude =@("c:\test\file1","c:\test\file2")
Invoke-Command -Session $remoteSession -ScriptBlock {
#$Using:exclude
Get-ChildItem -Path $Using:folder -recurse | Where {$Using:exclude -notcontains $_.FullName}
}
Remove-PSSession $remoteSession
Run Code Online (Sandbox Code Playgroud)
但是,如果我取消注释"$ Using:exclude",我会得到结果:

突然排除列表开始正常工作