powershell一些场景上的运用
powershell初始化系统
$cd = $NULL $cd = Get-WMIObject -Class Win32_CDROMDrive -ComputerName $env:COMPUTERNAME -ErrorAction Stop if ($cd.Drive -eq "D:") { Write-Output "Changing CD Drive letter from D: to O:" Set-WmiInstance -InputObject ( Get-WmiObject -Class Win32_volume -Filter "DriveLetter = 'd:'" ) -Arguments @{DriveLetter='O:'} } $disks=get-disk |where {$_.OperationalStatus -eq "Offline"} $DriversLetters=("D","E") #$DriversLetters=("E") $letterIndex=0 foreach ($d in $disks) { #Initialize-Disk -Number $d.Number -PartitionStyle GPT #$d|Initialize-Disk $d|Set-Disk -IsOffline $False New-Partition -DiskNumber $d.DiskNumber -UseMaximumSize -DriveLetter $DriversLetters[$letterIndex] New-Volume -FileSystem NTFS -DriveLetter $DriversLetters[$letterIndex] -DiskNumber $d.DiskNumber -FriendlyName "DATA" Format-Volume -DriveLetter $DriversLetters[$letterIndex] -FileSystem NTFS -NewFileSystemLabel "DATA" $letterIndex++ } Set-timezone "China Standard Time" Set-WinSystemLocale -SystemLocale "zh-CN" restart-computer
检查SQL集群服务状态GetSQLClusterServiceStatus
Import-Module failoverclusters $CurrNode = hostname; #$CurrNode $ActiveSQLNode = (get-clusterresource | ?{$_.resourcetype.name -eq "SQL Server"}).OwnerNode.Name #$ActiveSQLNode $SQLName = (get-clusterresource | ?{$_.resourcetype.name -eq "SQL Server"}).Name #$SQLName $return = 1 $SQLStatus = "" if($CurrNode -eq $ActiveSQLNode) { $SQLStatus = (Get-Service -Name $SQLName).Status if($SQLStatus -eq "Running") { $return = 0 } elseif ($SQLStatus -eq "paused") { $return = 1 } elseif ($SQLStatus -eq "starting") { $return = 2 } elseif ($SQLStatus -eq "pausing") { $return = 3 } elseif ($SQLStatus -eq "stopping") { $return = 5 } elseif ($SQLStatus -eq "stopped") { $return = 6 } else { $return = 0 } } else { $return = 200 } return $return
测试端口是否开启
Test-NetConnection -ComputerName TSTRsername -Port 22
powershell 批量迁移vcenter虚拟机
# Credentials $vcserver = "servername" $vcusername = "administrator@vsphere.local" $vcpassword = "password" # VM information $vmlist = Get-Content "serverlist.txt" # Login VMware Connect-VIServer -Server $vcserver -Protocol https -User $vcusername -Password $vcpassword # Get vmition $ClusterName = "datastore" $cluster = Get-Cluster -Name $ClusterName foreach ($vm in $vmList) { $esx = Get-VMHost -Location $cluster | Get-Random $ds = "vsanDatastore" VMware.VimAutomation.Core\Move-VM -VM $vm -Destination $esx -Datastore $ds }
windows批量安装zabbix
$ServerList = Get-Content C:\Temp\zabbix_install\server_list.txt $na = “” $p2 = ConvertTo-SecureString “” -AsPlainText -Force $Start = Get-Date $End = Get-Date foreach ($Server in $ServerList) { $A = New-Object System.Management.Automation.PSCredential($na,$p2) Invoke-Command -ComputerName $Server -Credential $A -ScriptBlock { Set-Location C:\temp invoke-expression -command C:\temp\zabbix_install.bat } Write-Host "$Server,$End,安装完成!" }
作者:随心所欲的南
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文链接,否则保留追究法律责任的权利。