2017-02-04 49 views
0

我想在一堆遠程計算機中找到註冊表項值。然後將值寫回到我的PC中的共享文件夾。但是,當我指定要寫入到我的PC中的共享文件夾的輸出時,出現拒絕訪問錯誤。我以管理員身份運行PowerShell。添加內容命令給我「訪問被拒絕」UnautorizedAccessException錯誤

$computers = Get-Content "C:\Temp\AutoSug\Computers.txt" 
$output_path = "\\mycomputername\powershell\output.csv" 
$setscript = 
{ 
    param($output_path) 
    $hostname = (Get-CIMInstance CIM_ComputerSystem).Name 
    $objExcel = New-Object -ComObject Excel.Application 
    if ($objExcel.Version -eq "12.0") 
    { 
     $HKEY_USERS = Get-ChildItem REGISTRY::HKEY_USERS | where-object { ($_.Name -like "*S-1-5-21*") -and ($_.Name -notlike "*_Classes") } 
     $Users = @() 
     $value = @() 
     foreach ($User in $HKEY_USERS) 
     { 
      $PROFILESID = Get-ChildItem REGISTRY::"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" | Where-Object { $_.name -like "*" + $USER.PSChildName + "*" } 
      $SID = $PROFILESID.PSChildName 
      foreach ($value in $SID) 
      { 
       $key = Get-Item REGISTRY::HKEY_USERS\$VALUE\Software\Microsoft\Office\12.0\Outlook\Preferences -ErrorAction SilentlyContinue 
       $gold = $key.property 
       if($gold -like 'ShowAutoSug') 
       { 
        $grail = (Get-ItemProperty REGISTRY::HKEY_USERS\$VALUE\Software\Microsoft\Office\12.0\Outlook\Preferences).ShowAutoSug 
        $objSID = New-Object System.Security.Principal.SecurityIdentifier($value) 
        $objUser = $objSID.Translate([System.Security.Principal.NTAccount]) 
        $hostname, $objUser, $value , $grail | Add-Content $output_path 
       } 
       else 
       { 
        $objSID = New-Object System.Security.Principal.SecurityIdentifier($value) 
        $objUser = $objSID.Translate([System.Security.Principal.NTAccount]) 
        $hostname,$objUser, $value , "The Auto Complete is not disabled" | Add-Content $output_path 
       } 
      } 
     } 
    } 
} 

foreach($computer in $computers) 
{ 
    Invoke-Command -ComputerName $computer -ScriptBlock $setscript -ArgumentList $output_path 
} 

確切的錯誤信息:

Access to the path '\\mycomputername\powershell\output.csv' is denied. 
FullyQualifiedErrorId : GetContentWriterUnauthorizedAccessError,Microsoft.PowerShell.Commands.AddContentCommand 
+0

hmm。不,這是複製粘貼錯誤..在我的代碼中有兩個反斜槓。 –

回答

1

的問題是,你正在做一個「第二跳」當你試圖將輸出寫入到共享文件夾,這需要將憑據委託給目標計算機。 您需要在本地主機和遠程計算機中使用Enable-WSManCredSSP命令啓用CredSSP。 然後你必須在invoke-command中使用CreddSSP。

Invoke-Command -ComputerName $computer -ScriptBlock $setscript 
-ArgumentList $output_path -Authentication Credssp -Credential (Get-Credential) 
+0

嗨。我想過這個問題。但共享文件夾位於我自己的計算機中。基本上沒有點「C」只有點A是我的電腦和點B是從其中註冊表值是從其中拉出並寫回到我的電腦的PC,我使用credssp的問題是它的安全問題。我無法將我的憑據傳遞給其他電腦。謝謝 –

+1

問題是你正在嘗試訪問b點中的共享文件夾,這使得「點c」。無論它是您的計算機還是其他計算機都無所謂,因爲您使用的是UNC路徑,因此您需要在「B點」使用憑據。 –

0

當我與遠程計算機交互時,我通常使用c $的完整路徑。不知道它是否適用於你的情況。所以:

"\\mycomputername\c$\powershell\output.csv"