【问题标题】:Powershell create folder on AzureVMPowershell 在 AzureVM 上创建文件夹
【发布时间】: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


【解决方案1】:

你是对的,为 Azure VM 创建一个文件夹,你可以使用Winrm

关于启用 Azure VM winrm,您应该将端口 5985 添加到 Azure NSG 入站规则,并将端口 5985 添加到 windows 防火墙 入站规则。

您可以使用此脚本创建会话:

$username = 'user'
$pass = ConvertTo-SecureString -string 'password' -AsPlainText -Force
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $pass
$s = New-PSSession -ConnectionUri 'http://xx.xx.xx.xx:5985' -Credential $cred -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)

关于新建文件夹,可以使用invoke-command来创建:

Invoke-Command -Session $s -ScriptBlock {new-item c:\newdirectory}

您还需要使用 Azure PowerShell 过滤 Azure VM 的公共 IP 地址。

这里a similar case关于你,通过Winrm运行powershell命令,请参考。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多