Azure powershell在远程系统中创建文件时权限被拒绝错误

时间:2017-10-05 15:26:40

标签: powershell azure azure-virtual-machine

我已经编写了以下代码来通过域凭据在远程系统中创建文件。 当我执行此代码时,我得到权限被拒绝错误。

代码:

$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

1 个答案:

答案 0 :(得分: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-address更改为HOSTNAME可解决此问题:

$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}