Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell UseDefaultCredentials不是';t在远程会话中工作_Powershell - Fatal编程技术网

Powershell UseDefaultCredentials不是';t在远程会话中工作

Powershell UseDefaultCredentials不是';t在远程会话中工作,powershell,Powershell,我正在尝试在远程计算机上运行invokewebrequest命令。 我使用的帐户是两台计算机上的本地管理员。 如果我登录到远程计算机并运行 调用WebRequest$url-UseDefaultCredentials,它可以完美地工作 然而,当我跑的时候 Invoke命令-ComputerName machineName-ScriptBlock{Invoke WebRequest“the path”-usefaultcredentials}在我的本地计算机上,它给我401-未经授权:由于凭据无效

我正在尝试在远程计算机上运行invokewebrequest命令。 我使用的帐户是两台计算机上的本地管理员。 如果我登录到远程计算机并运行
调用WebRequest$url-UseDefaultCredentials
,它可以完美地工作

然而,当我跑的时候
Invoke命令-ComputerName machineName-ScriptBlock{Invoke WebRequest“the path”-usefaultcredentials}
在我的本地计算机上,它给我401-未经授权:由于凭据无效,访问被拒绝。 我还尝试启动远程会话;
输入PSSession-ComputerName machineName
,然后只运行
调用WebRequest“路径”-UseDefaultCredentials
,但结果相同


为什么会这样?远程运行时,权限发生了哪些变化?如何在不提示输入密码和不将密码存储在脚本中的情况下解决此问题?

在远程服务器上运行以下命令:
启用WSManCredSSP-角色服务器–强制

(似乎您必须手动执行此操作;以下操作不起作用:

$remoteHosts | %{
    invoke-command -ComputerName $_ -Credential $cred -ScriptBlock {
        Enable-WSManCredSSP -Role Server –Force | out-null
    }
}
然后使用以下代码使本地计算机信任远程计算机,然后使用此信任调用它。 注意:如果您希望调用其他域中的远程计算机,请注释掉代码

[string[]]$remoteHosts = @('server1','server2')

$remoteHosts | %{Enable-WSManCredSSP -Role Client -DelegateComputer $_ -Force} | out-null

#if dealing with remote domains
#$trustedBefore = (Get-Item WSMan:\localhost\Client\TrustedHosts).value
#winrm set winrm/config/client ("@{{TrustedHosts='{0}'}}" -f (($remoteHosts + $remoteHosts) -join ',')) | out-null
$cred = get-credential
$remoteHosts | %{
    InvokeCommand -ScriptBlock {
        invoke-webrequest 'https://myUrl/' -UseDefaultCredentials | select StatusCode 
    } -Computer $_ -Credential $cred -Authentication Credssp
}
#winrm set winrm/config/client ("@{{TrustedHosts='{0}'}}" -f $trustedBefore) | out-null
参考:第二跳安全问题

Ref:不同域中的远程计算机

注意:您可能还会发现远程服务器位于不同的PS版本上(即,较旧的OS默认具有PS2)。 如果您了解到了这一点,下面是PS2的
Invoke WebRequest
的大致近似值(需要包含在脚本块中)

函数调用-PS2WebRequest{
param(
$Uri
)
试一试{
$request=[System.Net.HttpWebRequest]::Create($Uri)
$request.UseDefaultCredentials=$true
$request.PreAuthenticate=$true
$resp=$request.GetResponse()
$reqstream=$resp.GetResponseStream()
$sr=新对象System.IO.StreamReader$reqstream
$result=$sr.ReadToEnd()
写入输出(新对象-类型名PSObject-属性@{
StatusDescription=$resp.StatusDescription
StatusCode=[int]$resp.StatusCode
Content=$result
})
}抓住{
写入输出(新对象-类型名PSObject-属性@{
StatusDescription='错误'

StatusCode=[int]([regex]::matches($)。exception.message,”(?这可能是第二个跃点问题。请尝试在Invoke-Command上使用-Auth CredSSP。谢谢,但这仍然需要提供凭据。不过,看起来我无法避免这种情况。
function Invoke-PS2WebRequest {
    param(
        $Uri
    )
    try {
        $request = [System.Net.HttpWebRequest]::Create($Uri)
        $request.UseDefaultCredentials = $true
        $request.PreAuthenticate = $true
        $resp = $request.GetResponse()
        $reqstream = $resp.GetResponseStream()
        $sr = new-object System.IO.StreamReader $reqstream
        $result = $sr.ReadToEnd()
        write-output (new-object -TypeName PSObject -Property @{ 
            StatusDescription = $resp.StatusDescription
            StatusCode = [int]$resp.StatusCode
            Content = $result
        })
    } catch {
        write-output (new-object -TypeName PSObject -Property @{ 
            StatusDescription = 'ERROR'
            StatusCode = [int]([regex]::matches($_.exception.message, "(?<=\()[\d]{3}").Value)
            Content = $_.exception.message
        })        
    }
}