powershell - PowerShell - 在远程机器/计算机上调用命令
问题描述
有很多关于主题行的论坛/材料,但似乎无法为我的问题找到答案。
我正在尝试从主服务器(SRV01)执行一个脚本,该脚本将清理辅助服务器(SRV02、SRV03)上的临时文件夹。
这是脚本:
#Set the machines on the network to run the script on
$VDST = @("SRV02", "SRV03")
#Folder locations to clean out
$TempFolder = @("C:\Windows\Temp\*", "C:\Documents and Settings\*\Local Settings\temp\*")
#This function actually performs the clean up operation
Function executeCleanUp
{
$TempFolder = $args[0]
$machineNames = $args[2]
ForEach($machine in $machineNames){
Get-PSSession -ComputerName $machine | Format-Table -Property ComputerName, InstanceID
Write-Host 'Starting Clean Up...'
#Loop through the sub folders in the registry location
ForEach($folderLocation in $TempFolder)
{
$StrInput = 'Remove-Item -Path ' + $folderLocation + ' -Force -Recurse -ErrorAction SilentlyContinue'
$action = New-ScheduledTaskAction -Execute 'PowerShell.exe' -Argument $StrInput
Register-ScheduledTask -Action $action -TaskName "CleanUp"
Start-ScheduledTask -TaskName "CleanUp"
Unregister-ScheduledTask -TaskName "CleanUp" -Confirm:$false -ErrorAction SilentlyContinue
}
}
#Execute Script on specified machines - provided in array above
Invoke-Command -ComputerName $VDST -ScriptBlock ${function:executeCleanUp} -ArgumentList $TempFolder, $VDST
运行上述内容后,我收到错误消息:
指定的登录会话不存在
因此,我遇到了一个论坛,建议执行以下操作:
#Remote Server (VDI)
Enable-WSManCredSSP -Role server
#Expected Output
#This computer is configured to receive credentials from a remote client computer.
#Local Machine
Enable-WSManCredSSP -Role Client -DelegatedCredentials 'SRV01'
#Expected Output
#The machine is configured to allow delegating fresh credentials to the following target(s): wsman/SRV01.
#Local Machine
#Open gpedit.msc
#Browse to Computer Configuration > Administrative Templates > System > Credentials Delegation.
#Double-click "Allow delegating fresh credentials with NTLM-only Server Authentication"
#Enable the setting
#Add the build server to the server list as WSMAN/BuildServerName.
#Example Execution:
#Invoke-Command -ComputerName <REMOTE_COMPUTER_NAME> -Authentication CredSSP -Credential <USERNAME> -ScriptBlock { #code}
我已经完成了所有这些,但现在我得到了错误:
计算机策略不允许将用户凭据委派给目标计算机
另外,我假设这条线
WSMAN/BuildServerName
应该写
WSMAN/SRV02
解决方案
出现了 2 跳身份验证问题,因为您试图在远程会话中列出远程会话
Get-PSSession -ComputerName $machine | Format-Table -Property ComputerName, InstanceID
如果你只是想清除远程服务器上的一些文件,下面的代码应该可以在不需要 CredSPP 的情况下工作。
设置-ErrorAction SilentlyContinue
会使故障排除变得困难,在尝试删除文件之前检查文件是否存在更容易。
$TempFolder = $args[0]
$ComputerArray = "SRV02","SRV03"
$ScriptBlock =
{
foreach ($Folder in $TempFolders)
{
if (Test-Path -Path $TempFolder)
{
Remove-Item -Path $Folder -force
}
}
}
Invoke-Command -ComputerName $ComputerArray -ScriptBlock $ScriptBlock -ArgumentList $TempFolder
错误的答案:
您的问题是两跳身份验证。您不能使用默认 Windows 设置嵌套远程会话。虽然这不被认为是最佳实践,但您可以启用 CredSSP 绕过该问题。
https://docs.microsoft.com/en-us/windows/win32/secauthn/credential-security-support-provider
您可以登录 SVR01 运行脚本还是从您自己的计算机上针对目标计算机运行脚本?
推荐阅读
- r - 使用 tidyverse 根据另一个变量的字母顺序绘制带有要排序的因子的图形
- spyder - 如何在 Spyder 中禁用自动函数文档字符串生成?
- python-3.x - 在 python 中使用 BeautifulSoup 从网站上抓取报告
- numpy - 尝试打开包含在 Pyroot 中的根树中的二维数组的问题
- html - flex-flow:换行 - 显示为列而不是行
- typescript - 我应该使用任何类型还是将类型留空?
- spring - Spring JPA:构造函数的参数 0 ... 需要类型为 ... 的 bean,但无法找到
- android - 切换一个按钮并取消切换另一个按钮
- mysql - 这个 MySQL 存储过程中的语法错误在哪里?
- reactjs - react-intl v4升级`IntlProvider`未找到阻止组件在使用`injectIntl`包装器时显示