微软Azure PowerShell使用指南(2) - 控制虚拟机

如上篇,选择相应的订阅信息后,我们就可以开始使用我们的云资源。这篇我们介绍如何创建虚拟机并且通过PowerShell就可以控制虚拟机。


创建虚拟机

微软提供了非常多的操作系统镜像,从Windows 2008 R2到最新的Windows 2012 R2,从OpenLogic到Ubuntu 14.04,而且镜像还在不断增加。可以通过以下命令来找到当前可用的镜像信息:

get-AzureVMImage | select ImageName

目前中国云可用的镜像有:

0c5c79005aae478e8883bf950a861ce0__Windows-Server-2012-Essentials-20131018-enus
0c5c79005aae478e8883bf950a861ce0__Windows-Server-2012-Essentials-20131018-zhcn
0c5c79005aae478e8883bf950a861ce0__Windows-Server-2012-Essentials-20140115-enus
0c5c79005aae478e8883bf950a861ce0__Windows-Server-2012-Essentials-20140115-zhcn
0c5c79005aae478e8883bf950a861ce0__Windows-Server-2012-Essentials-20140213-enus
0c5c79005aae478e8883bf950a861ce0__Windows-Server-2012-Essentials-20140213-zhcn
0c5c79005aae478e8883bf950a861ce0__Windows-Server-2012-Essentials-20140306-enus
0c5c79005aae478e8883bf950a861ce0__Windows-Server-2012-Essentials-20140306-zhcn
0c5c79005aae478e8883bf950a861ce0__Windows-Server-2012-Essentials-20140327-enus
0c5c79005aae478e8883bf950a861ce0__Windows-Server-2012-Essentials-20140327-zhcn
0c5c79005aae478e8883bf950a861ce0__Windows-Server-2012-Essentials-20140618-enus
0c5c79005aae478e8883bf950a861ce0__Windows-Server-2012-Essentials-20140618-zhcn
0c5c79005aae478e8883bf950a861ce0__Windows-Server-2012-Essentials-20140715-enus
0c5c79005aae478e8883bf950a861ce0__Windows-Server-2012-Essentials-20140715-zhcn
250d269906be4694a10aee49a3385f2d__SUSE-Linux-Enterprise-Server-11-SP3-v203
55bc2b193643443bb879a78bda516fc8__Win2K8R2SP1-Datacenter-201306.01-en.us-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Win2K8R2SP1-Datacenter-201306.01-zh.cn-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Win2K8R2SP1-Datacenter-201311.01-en.us-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Win2K8R2SP1-Datacenter-201312.01-en.us-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Win2K8R2SP1-Datacenter-201312.01-zh.cn-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Win2K8R2SP1-Datacenter-201401.01-en.us-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Win2K8R2SP1-Datacenter-201401.01-zh.cn-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Win2K8R2SP1-Datacenter-201402.01-en.us-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Win2K8R2SP1-Datacenter-201402.01-zh.cn-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Win2K8R2SP1-Datacenter-201403.01-en.us-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Win2K8R2SP1-Datacenter-201403.01-zh.cn-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Win2K8R2SP1-Datacenter-201406.01-en.us-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Win2K8R2SP1-Datacenter-201406.01-zh.cn-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-Datacenter-201306.01-en.us-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-Datacenter-201306.01-zh.cn-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-Datacenter-201311.01-en.us-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-Datacenter-201311.01-zh.cn-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-Datacenter-201312.01-en.us-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-Datacenter-201312.01-zh.cn-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-Datacenter-201401.01-en.us-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-Datacenter-201401.01-zh.cn-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-Datacenter-201402.01-en.us-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-Datacenter-201402.01-zh.cn-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-Datacenter-201403.01-en.us-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-Datacenter-201403.01-zh.cn-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-Datacenter-201406.01-en.us-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-Datacenter-201406.01-zh.cn-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-R2-201310.01-zh.cn-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-R2-201311.01-en.us-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-R2-201311.01-zh.cn-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-R2-201312.01-en.us-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-R2-201312.01-zh.cn-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-R2-201401.01-en.us-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-R2-201401.01-zh.cn-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-R2-201402.01-en.us-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-R2-201402.01-zh.cn-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-R2-201403.01-en.us-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-R2-201403.01-zh.cn-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-R2-201406.01-en.us-127GB.vhd
55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-R2-201406.01-zh.cn-127GB.vhd
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2008R2-SP2-10.50.4319.0-Enterprise-CHS-Win2008R2-cy14su
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2008R2-SP2-10.50.4319.0-Enterprise-ENU-Win2008R2-cy14su
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2008R2-SP2-10.50.4319.0-Standard-CHS-Win2008R2-cy14su05
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2008R2-SP2-10.50.4319.0-Standard-ENU-Win2008R2-cy14su05
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2008R2-SP2-10.50.4319.0-Web-CHS-Win2008R2-cy14su05
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2008R2-SP2-10.50.4319.0-Web-ENU-Win2008R2-cy14su05
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2008R2SP2-10.50.4021.0-Enterprise-ENU-WS2008R2
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2008R2SP2-10.50.4021.0-Standard-ENU-WS2008R2
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2008R2SP2-GDR-10.50.4021.0-Enterprise-CHS-Win2K8R2-CY13
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2008R2SP2-GDR-10.50.4021.0-Enterprise-CHS-Win2K8R2-CY13
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2008R2SP2-GDR-10.50.4021.0-Enterprise-CHS-Win2K8R2-CY14
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2008R2SP2-GDR-10.50.4021.0-Enterprise-ENU-Win2K8R2-CY13
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2008R2SP2-GDR-10.50.4021.0-Enterprise-ENU-Win2K8R2-CY14
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2008R2SP2-GDR-10.50.4021.0-Standard-CHS-Win2K8R2-CY13SU
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2008R2SP2-GDR-10.50.4021.0-Standard-CHS-Win2K8R2-CY13SU
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2008R2SP2-GDR-10.50.4021.0-Standard-CHS-Win2K8R2-CY14SU
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2008R2SP2-GDR-10.50.4021.0-Standard-ENU-Win2K8R2-CY13SU
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2008R2SP2-GDR-10.50.4021.0-Standard-ENU-Win2K8R2-CY14SU
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2008R2SP2-GDR-10.50.4021.0-Web-ENU-Win2K8R2-CY13SU12
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2008R2SP2-GDR-10.50.4021.0-Web-ENU-Win2K8R2-CY14SU02
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2012-SP1-11.0.3430.0-Enterprise-CHS-Win2012-cy14su05
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2012-SP1-11.0.3430.0-Enterprise-ENU-Win2012-cy14su05
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2012-SP1-11.0.3430.0-Standard-CHS-Win2012-cy14su05
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2012-SP1-11.0.3430.0-Standard-ENU-Win2012-cy14su05
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2012-SP1-11.0.3430.0-Web-CHS-Win2012-cy14su05
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2012-SP1-11.0.3430.0-Web-ENU-Win2012-cy14su05
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2012-SP1-11.0.3446.0-DataWarehousing-CHS-Win2012-cy14su
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2012-SP1-11.0.3446.0-DataWarehousing-ENU-Win2012-cy14su
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2012SP1-CU5-11.0.3373.0-DataWarehousing-CHS-WS2012-CY13
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2012SP1-CU5-11.0.3373.0-DataWarehousing-ENU-WS2012
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2012SP1-CU5-11.0.3373.0-DataWarehousing-ENU-WS2012-CY13
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2012SP1-CU5-11.0.3373.0-Enterprise-CHS-Win2012-CY13SU12
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2012SP1-CU5-11.0.3373.0-Enterprise-ENU-Win2012-CY13SU12
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2012SP1-CU5-11.0.3373.0-Standard-CHS-Win2012-CY13SU12
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2012SP1-CU5-11.0.3373.0-Standard-ENU-Win2012-CY13SU12
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2012SP1-CU5-11.0.3393.0-DataWarehousing-CHS-WS2012-CY14
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2012SP1-CU5-11.0.3393.0-DataWarehousing-ENU-WS2012-CY14
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2012SP1-Enterprise-SQL11-SP1-CU3-11.0.3350.0-Win2012-CH
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2012SP1-Enterprise-SQL11-SP1-CU3-11.0.3350.0-Win2012-EN
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2012SP1-Standard-SQL11-SP1-CU3-11.0.3350.0-Win2012
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2012SP1-Standard-SQL11-SP1-CU4-11.0.3368.0-Win2012-CHS
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2014-RTM-12.0.2361.0-DataWarehousing-CHS-Win2012R2-cy14
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2014-RTM-12.0.2361.0-Enterprise-CHS-Win2012R2-cy14su05
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2014-RTM-12.0.2361.0-Enterprise-ENU-Win2012R2-cy14su05
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2014-RTM-12.0.2361.0-Standard-CHS-Win2012R2-cy14su05
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2014-RTM-12.0.2361.0-Standard-ENU-Win2012R2-cy14su05
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2014-RTM-12.0.2361.0-Web-CHS-Win2012R2-cy14su05
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2014-RTM-12.0.2361.0-Web-ENU-Win2012R2-cy14su05
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2014RTM-12.0.2000.8-DataWarehousing-CHS-WS2012R2-AprilG
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2014RTM-12.0.2000.8-DataWarehousing-ENU-WS2012R2-AprilG
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2014RTM-12.0.2000.8-ENTCORE-CHS-WS2012R2-AprilGA
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2014RTM-12.0.2000.8-ENTCORE-ENU-WS2012R2-AprilGA
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2014RTM-12.0.2000.8-Standard-CHS-WS2012R2-AprilGA
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2014RTM-12.0.2000.8-Standard-ENU-WS2012R2-AprilGA
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2014RTM-12.0.2000.8-Web-CHS-WS2012R2-AprilGA
74bb2f0b8dcc47fbb2914b60ed940c35__SQL-Server-2014RTM-12.0.2000.8-Web-ENU-WS2012R2-AprilGA
9619bdbee1584b6f80d684565a6eeb74__SharePoint-2013-Trial-3-26-2014
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_04_2-LTS-amd64-server-20130225-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_04_2-LTS-amd64-server-20130325-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_04_2-LTS-amd64-server-20130415-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_04_2-LTS-amd64-server-20130527-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_04_2-LTS-amd64-server-20130603-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_04_2-LTS-amd64-server-20130624-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_04_3-LTS-amd64-server-20130827-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_04_3-LTS-amd64-server-20130909-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_04_3-LTS-amd64-server-20130916.1-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_04_3-LTS-amd64-server-20131003-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_04_3-LTS-amd64-server-20131024-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_04_3-LTS-amd64-server-20131111-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_04_3-LTS-amd64-server-20131114-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_04_3-LTS-amd64-server-20131205-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_04_3-LTS-amd64-server-20140130-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_04_4-LTS-amd64-server-20140529-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_04_4-LTS-amd64-server-20140606-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_04_4-LTS-amd64-server-20140619-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_04_4-LTS-amd64-server-20140702-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_10-amd64-server-20130227-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_10-amd64-server-20130414-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_10-amd64-server-20130526-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_10-amd64-server-20130616-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_10-amd64-server-20130707-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_10-amd64-server-20130821-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_10-amd64-server-20130908-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_10-amd64-server-20130929-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_10-amd64-server-20131110-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_10-amd64-server-20131113-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_10-amd64-server-20131211-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_10-amd64-server-20140202-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_10-amd64-server-20140212-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-12_10-amd64-server-20140302-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-13_10-amd64-server-20131015-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-13_10-amd64-server-20131113-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-13_10-amd64-server-20131204-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-13_10-amd64-server-20131215-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-13_10-amd64-server-20140108-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-13_10-amd64-server-20140119-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-13_10-amd64-server-20140129-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-13_10-amd64-server-20140202-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-13_10-amd64-server-20140212-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-13_10-amd64-server-20140409.1-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-13_10-amd64-server-20140427-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-13_10-amd64-server-20140507-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-13_10-amd64-server-20140604-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-13_10-amd64-server-20140608-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-14_04-LTS-amd64-server-20140226.1-beta1-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-14_04-LTS-amd64-server-20140416.1-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-14_04-LTS-amd64-server-20140528-en-us-30GB
b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-14_04-LTS-amd64-server-20140606.1-en-us-30GB
f1179221e23b4dbb89e39d70e5bc9e72__OpenLogic-CentOS-65-20140710


镜像名称里含有OS名称、版本等信息。


因为接下来要演示用PowerShell来操作虚拟机,这里我们选择一个Windows镜像:

55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-R2-201406.01-zh.cn-127GB.vhd

这里还需要查询一下我们的虚拟机在放在哪个位置的数据中心里。运行以下命令可以列出所有可用的位置:

get-azurelocation | select name
中国云返回:
China North
China East

目前中国云有北京和上海两个地点,对应第一个和第二个值。


为了创建虚拟机,还需要设置一个默认存储账号,用于存放虚拟机硬盘。运行以下命令创建一个存储账号并且设置为当前订阅的默认存储账号:

$storageAccountName = "<你的存储账号名称>"
$location = "China East"
New-AzureStorageAccount -StorageAccountName $storageAccountName -Location $location
$subscriptionName = (Get-AzureSubscription | ? { $_.isdefault}).SubscriptionName
Set-AzureSubscription -CurrentStorageAccountName $storageAccountName -SubscriptionName $SubscriptionName

现在我们在上海创建一台Windows虚拟机,运行以下命令即可:

$VMSize = "Medium"
$image="55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-R2-201406.01-zh.cn-127GB.vhd"
$username = "<登录用户名>"
$password = "<密码>"
$serviceName = "<服务名称>"

New-AzureService $serviceName -location $location
New-AzureVMConfig -Name $serviceName -InstanceSize $VMSize -ImageName $image  | Add-AzureProvisioningConfig –Windows -AdminUsername $username –Password $password | New-AzureVM –ServiceName $serviceName

稍等片刻,虚拟机就可以使用了。在门户网站上就可以通过远程桌面连接了,下面我们介绍用PowerShell来完成这些任务。


用PowerShell连接并控制虚拟机


首先,我们需要将这台虚拟机的证书导入本地计算机,使得本机可以通过PowerShell连接。在以管理员身份打开的PowerShell窗口中运行以下命令:

$WinRWCert = (Get-AzureVM -ServiceName $ServiceName -Name $ServiceName | select -ExpandProperty vm).DefaultWinRMCertificateThumbprint
$AzureX509cert = Get-AzureCertificate -ServiceName $ServiceName -Thumbprint $WinRWCert -ThumbprintAlgorithm sha1
$certTempFile = [IO.Path]::GetTempFileName()
$AzureX509cert.Data | out-file $certTempFile

$certToImport = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $certTempFile
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store "Root","LocalMachine"
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
$store.Add($certToImport)
$store.Close()
rm $certTempFile


完成后,我们就可以取到PSSession:

#找出PowerShell的随机端口
$port = (Get-AzureVM -ServiceName $ServiceName -Name $VMName | Get-AzureEndpoint | ? { $_.Name -eq "WinRmHTTPs"}).Port 
if ($port -eq $null)  {
    $port = (Get-AzureVM -ServiceName $ServiceName -Name $VMName | Get-AzureEndpoint | ? { $_.Name -eq "PowerShell"}).Port
}
$computerName = "$ServiceName.chinacloudapp.cn" #中国云的地址,国际云用:xx.cloudapp.net
$pass= ConvertTo-SecureString $password -AsPlainText -force
$cred=New-Object -TypeName system.management.automation.pscredential -argument $username,$pass
$session = New-PSSession -ComputerName $computerName -Port $port -Credential $cred -UseSSL

在虚拟机里运行PowerShell命令:

invoke-command -Session $session -ScriptBlock { ls c:\ }

这条命令将在虚拟机里运行

ls c:\
将其换成你需要执行的PowerShell命令即可。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值