2017-10-05 77 views
0

我已經編寫了下面的代碼,以通過域憑據在遠程系統中創建文件。 當我執行此代碼時,我得到權限被拒絕的錯誤。Azure powershell在遠程系統中創建文件時,權限被拒絕錯誤

代碼:

$username = "domain\username" 
$password = "Welcome1234$" 
$secstr = New-Object -TypeName System.Security.SecureString 
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)} 
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr 
Invoke-Command -Credential $cred -Computer VM1{ 
New-Item \\VM2\sapmnt\SID\SYS\profile\test.txt -ItemType file 
} 

錯誤:

Access to the path '\\VM2\sapmnt\SID\SYS\profile\test.txt' is denied. 
    + CategoryInfo   : PermissionDenied: (\\VM2\s...rofile\test.txt:String) [New-Item], UnauthorizedAccessException 
    + FullyQualifiedErrorId : NewItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand 
+0

您可以使用該帳戶訪問'\\ VM2 \ sapmnt \ SID \ SYS \ profile'嗎?該帳戶是否有權限(讀/寫)? –

回答

0

您可以使用該帳戶訪問路徑\\VM2\sapmnt\SID\SYS\profile?您是否有權限閱讀

我在我的實驗室進行過測試,它適用於我。

授予權限(讀/寫)到該帳戶:

enter image description here

下面是腳本:

$username = 'jason' 
$pass = ConvertTo-SecureString -string 'password' -AsPlainText -Force 
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $pass 
$s = New-PSSession -ConnectionUri 'http://13.73.23.129:5985' -Credential $cred -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck) 
Invoke-Command -Session $s -ScriptBlock {new-item \\jasonvm\profile\jasontest3.txt} 

更新

改變ip-addressHOSTNAME解決此問題:

$username = 'jason' 
$pass = ConvertTo-SecureString -string 'password' -AsPlainText -Force 
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $pass 
$s = New-PSSession -ConnectionUri 'VM2hostname:5985'; -Credential $cred -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck) 
Invoke-Command -Session $s -ScriptBlock {new-item \\jasonvm\profile\jasontest3.txt} 
+0

當我執行它作爲天藍色的自定義代碼擴展。我得到了以下錯誤。 服務器失敗\\ n帶有以下錯誤消息:WinRM客戶端無法處理\\ nrequest。默認身份驗證可以在以下條件下與IP地址一起使用:傳輸爲HTTPS或目標位於\\ nTrustedHosts列表中,並提供顯式憑證。將winrm.cmd用於\\ n配置TrustedHosts。請注意,TrustedHosts列表中的計算機可能未經過身份驗證。 – hans

+0

通過將ip-address更改爲主機名來解決此問題。 – hans

+0

通過將IP地址更改爲HOSTNAME來解決此問題。 $ s = New-PSSession -ConnectionUri'http:// VM2hostname:5985'-Credential $ cred -SessionOption(New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck) – hans

相關問題