Windows 使用Powershell在远程计算机上创建文件夹-如何添加凭据?
我使用下面的powershell脚本尝试在远程计算机上创建文件夹Windows 使用Powershell在远程计算机上创建文件夹-如何添加凭据?,windows,powershell,Windows,Powershell,我使用下面的powershell脚本尝试在远程计算机上创建文件夹 $ComputerName = "<IP>" $DriveLetter = "C" $Path = "TempFolder\TestPath" New-Item -Path \\$ComputerName\$DriveLetter$\$Path -type directory -Force $ComputerName=“” $DriveLetter=“C”
$ComputerName = "<IP>"
$DriveLetter = "C"
$Path = "TempFolder\TestPath"
New-Item -Path \\$ComputerName\$DriveLetter$\$Path -type directory -Force
$ComputerName=“”
$DriveLetter=“C”
$Path=“TempFolder\TestPath”
新项目-路径\\$ComputerName\$DriveLetter$\$Path-类型目录-强制
当我将ComputerName更改为localhost时,将执行脚本并创建所需的文件夹。但是,当我为同一网络中的IP运行此脚本(我可以ping)时,脚本会运行,但不会创建文件夹
是否需要在脚本中的某个位置提供远程计算机的登录凭据?确实需要:
PS C:> help new-item
NAME
New-Item
SYNTAX
New-Item [-Path] <string[]> [-ItemType <string>] [-Value <Object>] [-Force] [-Credential <pscredential>] [-WhatIf]
[-Confirm] [-UseTransaction] [<CommonParameters>]
New-Item [[-Path] <string[]>] -Name <string> [-ItemType <string>] [-Value <Object>] [-Force] [-Credential
<pscredential>] [-WhatIf] [-Confirm] [-UseTransaction] [<CommonParameters>]
ALIASES
ni
如果您在网上搜索如何创建
PSCredential
对象,还有其他更安全的方法可以做到这一点。谢谢您的回复!将是用户名和密码的组合,类似于“.\admin”abc@123“?”这是一个很好的问题,补充到了答案中。
$password = ConvertTo-SecureString 'password' -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ('username', $password)