我已经编写了以下代码来通过域凭据在远程系统中创建文件。 当我执行此代码时,我得到权限被拒绝错误。
代码:
$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 :(得分:0)
您可以使用该帐户访问该路径\\VM2\sapmnt\SID\SYS\profile
吗?您是否有权阅读或撰写?
我在我的实验室进行过测试,它适用于我。
向该帐户授予权限(读/写):
这是脚本:
$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}