使用powershell创建ARM虚机

表格下方为powershell执行内容,过程中需要调用"D:\vm00.csv"文件,请提前准备。一定要注意为CSV文件格式

vm00.csv内容:
在这里插入图片描述

#Import-Module -name Azure
#Get-Module
#Login-AzureRmAccount -EnvironmentName AzureChinaCloud
#Get-AzureRmSubscription
##Select-AzureSubscription -SubscriptionId XXX #选择当前订阅
#Get-AzureRmVMSize -Location "ChinaNorth2"  #查询虚机型号
#Get-AzureRmVMImage -Location "ChinaNorth2" -PublisherName OpenLogic -Offer CentOS -Skus 7.4

Import-Csv -Path "D:\vm00.csv" |foreach{
 #所在地区
 $location = $_.location
 $vmName = $_.vmName
 $vmSise = $_.size
 #存储账户名称
 $storageName =  $_.storageName
 #可用性集
 $AvailabilitySetName = $_.aset
 #登录名
 $User = $_.userName
 #公网IP
 $pipName=$vmName + "-pip"
 #内网IP
 $dip = $_.dip
 #密码
 $PWord = ConvertTo-SecureString -String $_.passwd -AsPlainText -Force
 $Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User, $PWord
 #子网名称
 $subnetName = $_.subnetName
 $subaddress=$_.subaddress
 #资源组名称
 $rg = $_.resourceGroup
 #虚拟网络名称
 $vnetName = $_.vnetName
 $address=$_.addressip
 #将vm加入到另外一个虚拟网络
 #$vnetrg = $_.vnetrg
 #网络安全组
 $securitygroup=$_.securitygroup
 #数据盘
 $datadisk = $_.datadisk
 #镜像
 $image=$_.image
 
  "Power Shell Begin"
    
    New-AzureRmResourceGroup -Name $rg -Location $location
    $cred = $Credential 
    $vip = $vmName
    
    $nicName = $vmName 
   
    $vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rg -Location $location -AddressPrefix $address -Subnet $subnet
    #$vnetrg
    $subnet = New-AzureRmVirtualNetworkSubnetConfig -Name $subnetName  -AddressPrefix $subaddress
    # -VirtualNetwork $vnet
    Set-AzureRmVirtualNetwork -VirtualNetwork $vnet
    $vnet = get-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rg
    $pip = New-AzureRmPublicIpAddress -ResourceGroupName $rg -Location $location -AllocationMethod Static -Force -IpAddressVersion IPv4 -Name $pipName
    # Create an inbound network security group rule for port 22
    $nsgRuleSSH = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleSSH  -Protocol Tcp `
    -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
    -DestinationPortRange 22 -Access Allow
 
    # Create an inbound network security group rule for port 80
    $nsgRuleWeb = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleWWW  -Protocol Tcp `
    -Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
    -DestinationPortRange 80 -Access Allow
 
    # Create a network security group
    #$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName $rg -Location $location -Name $securitygroup -SecurityRules $nsgRuleSSH,$nsgRuleWeb
    
    $nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rg -Location $location -SubnetId $vnet.Subnets[0].Id -Force  -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id
    if(($aset = Get-AzureRmAvailabilitySet -ResourceGroupName $rg -Name  $AvailabilitySetName -ErrorAction SilentlyContinue) -eq $null){
       $aset = New-AzureRmAvailabilitySet -ResourceGroupName $rg -Name  $AvailabilitySetName -Location $location -Sku aligned -PlatformFaultDomainCount 2 -PlatformUpdateDomainCount 2
       #-Sku aligned
    }
   
 
    $vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSise -AvailabilitySetId $aset.Id 
    $computerName = $vmName
    $vm = Set-AzureRmVMOperatingSystem -Linux -VM $vmConfig -ComputerName $computerName -Credential $cred 
    $vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
  
    if($image -eq 7){
        $vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName OpenLogic -Offer CentOS -Skus 7.4 -Version 7.4.20180704
    }
    if($image -eq 6){
        $vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName OpenLogic -Offer CentOS -Skus 6.5 -Version 6.5.20150904
    }
  
    New-AzureRmVM -ResourceGroupName $rg -Location $location -VM $vmConfig
    Get-AzureRmVM -ResourceGroupName $rg -Name $vmName    
    "Power Shell End" 
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在 PowerShell创建快捷方式,您需要使用 New-Object cmdlet 和 WScript.Shell COM 对象。 示例代码如下: ``` $WshShell = New-Object -ComObject WScript.Shell $Shortcut = $WshShell.CreateShortcut("C:\Path\To\Shortcut.lnk") $Shortcut.TargetPath = "C:\Path\To\Target.exe" $Shortcut.Save() ``` 在这里,$Shortcut 变量保存着新创建的快捷方式的对象。然后,您可以使用 TargetPath 属性指定快捷方式指向的目标文件的路径。最后,使用 Save() 方法将快捷方式保存到指定的路径。 您还可以使用其他属性来设置快捷方式的其他属性,例如 Description、WorkingDirectory 等。有关详细信息,请参阅 MSDN 文档:https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/8k1kstfh(v=vs.84) 希望这能帮助您。 ### 回答2: 在PowerShell创建快捷方式的方法是通过使用`New-Object`命令和`WScript.Shell`对象来完成。下面是详细的步骤: 1. 打开PowerShell控制台。 2. 使用以下命令创建一个`WScript.Shell`对象: ```powershell $shell = New-Object -ComObject WScript.Shell ``` 这会实例化一个`WScript.Shell`对象,并将其赋给`$shell`变量。 3. 使用`$shell.CreateShortcut`方法来创建快捷方式。指定快捷方式的目标、快捷方式的名称和快捷方式的保存位置。例如: ```powershell $shortcut = $shell.CreateShortcut("C:\Users\用户名\Desktop\新快捷方式.lnk") ``` 这将在桌面上创建一个名为"新快捷方式"的快捷方式。 4. 设置快捷方式的目标路径和其他属性。例如,如果要指定计算器应用程序作为快捷方式的目标,则可以使用以下命令: ```powershell $shortcut.TargetPath = "C:\Windows\System32\calc.exe" ``` 5. 使用`$shortcut.Save()`方法保存创建的快捷方式。 ```powershell $shortcut.Save() ``` 6. 最后,关闭PowerShell控制台。 通过按照上述步骤在PowerShell创建快捷方式,您可以为特定应用程序或文件创建自定义的桌面或文件夹快捷方式。 ### 回答3: 要使用Powershell创建快捷方式,可以按照以下步骤进行: 1. 打开Powershell控制台:在开始菜单中搜索“powershell”,然后选择“Windows Powershell”。 2. 输入以下命令以创建快捷方式: ``` $WshShell = New-Object -ComObject WScript.Shell $Shortcut = $WshShell.CreateShortcut("C:\路径\快捷方式.lnk") $Shortcut.TargetPath = "目标文件的路径" $Shortcut.IconLocation = "图标文件的路径" $Shortcut.WorkingDirectory = "快捷方式的工作目录" $Shortcut.Save() ``` 确保替换“C:\路径\快捷方式.lnk”为您想要创建快捷方式的路径,将“目标文件的路径”替换为快捷方式指向的文件路径,将“图标文件的路径”替换为快捷方式的图标路径,而“快捷方式的工作目录”是可选的,可以设置为期望的目录。 3. 运行命令后,将在指定路径中创建带有给定名称的快捷方式。您可以在资源管理器中导航到该路径以查看此快捷方式。 使用Powershell可以轻松地创建快捷方式,并且可以通过更改上述命令中的属性来自定义快捷方式的目标、图标和工作目录等属性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值