【发布时间】:2018-05-08 17:47:00
【问题描述】:
我一直在尝试使用 powershell 在 Azure 中的虚拟机列表上创建一个文件夹。
在虚拟机中我可以使用下面的工作
$c = Get-Credential -Credential domain\AdminUser
$Comp = Get-Content C:\temp\list.txt
$s = $ExecutionContext.InvokeCommand.NewScriptBlock("mkdir c:\Whatyousaying")
Invoke-Command -ComputerName $Comp -ScriptBlock $s -Credential $c
但是我尝试了一些方法,但它无法识别 AzureVm 路径。
我尝试过类似的东西
$ComputerName = Get-AzureRmVM | select Name
$DriveLetter = "C"
$Path = "NewDirectory"
foreach ($ComputerNames in $ComputerName)
{
New-Item -Path \\
$ComputerName\$DriveLetter$\$Path -type directory -Force
}
我遗漏了一些明显的东西,我认为 DNS 找不到服务器,所以我尝试使用公共 ip 名称和地址,但它们因 winRM 错误而失败,但这在 Azure 中有效。我已经使用 Enable-PSRemoting 进行了检查,该功能已启用。
提前致谢
【问题讨论】:
-
您似乎缺少转义字符 ` 以将 $ 包含在您的共享路径中。将您的 New-Item 行更改为 New-Item "\\$ComputerName\$DriveLetter`$\$Path" 。提示:当您遇到共享路径问题时,只需将命令更改为 Write-Host 以确保它是您想要的路径。然后,您可以获取该命令的输出并将其放入 Run Box 以确保它有效。我为格式道歉。我无法正确打印反引号。
标签: powershell azure