本文介绍了Powershell - 需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个脚本,用于在修补之前对我们所有的服务器进行快照(我们有200多个),我的脚本应该检查名称,如果名称中没有字符串则不应该修补,但我无法检查名称。  I
几乎使用了我能想到的每一个比较,但我对powershell还是比较新的。

 Connect-VIServer -User" someusername" -Password" *********" -Server"FQDN" 
$ VMS = get-vm |其中{($ _。PowerState -eq" PoweredOn")}
$ date = Get-Date -Format dd-MMM-yyyy

$ number = $ VMS.Count
$ num = 1
$ time =获取日期
foreach($ vm in $ vms){
If($ vm.name -notcontains" _rogue_"){
New-Snapshot -VM" $ VM" -Name(" $ date" +" _Patch_cycle")-Description"在Patch cycle $ date之前拍摄的自动捕捉" -Memory
write-host"快照$ VM已完成,$ num of $ number @ $ time"
$ num = $ num +1}
}

快照药水有效,它只是捕捉每个主机,我有20 - 30主机_rogue_在名称的中间,我想不要拍,因为他们并不真的需要它...理想情况下我有两个字符串我想排除,但我无法得到
singe字符串工作更不用说2个字符串



解决方案

I'm building a script to snapshot all of our servers just before patching (we have over 200), my script is supposed to check the name and if it does not have a string in the name its not supposed to patch, but I can not get it to check the name.  I have used just about every comparison I can think of, but I'm fairly new to powershell.

Connect-VIServer -User "someusername" -Password "*********" -Server "FQDN"
$VMS = get-vm | where { ($_.PowerState -eq "PoweredOn") }
$date=Get-Date -Format dd-MMM-yyyy

$number = $VMS.Count
$num = 1
$time = Get-Date
foreach ($vm in $vms) {
    If ($vm.name -notcontains "_rogue_") {
    New-Snapshot -VM "$VM" -Name ("$date" + "_Patch_cycle") -Description "Auto Snap taken before Patch cycle $date" -Memory
    write-host "Snapshot on $VM completed, $num of $number @ $time"
    $num = $num +1}
    } 

the snap potion works, it's just snapping every host, and I have 20 - 30 host with _rogue_ in the middle of the name, thta I would like to NOT snap, as they don't really need it... Ideally I have two Strings I would like to exclude, but I could not get the singe string to work let alone 2 strings

解决方案


这篇关于Powershell - 需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 23:36
查看更多