Three questions about correctly structuring Where-Object statements
i'm writing script searches , modifies lines within .vmx file (functionally same config file). i'm new type of scripting , having trouble wrapping head around of behavior i'm seeing.
first, i'm searching specific type of config line within file. this line typically looks (but not typically adjacent 1 another):
ethernet0.virtualdev = "e1000"
ethernet1.virtualdev = "e1000"
ethernet2.virtualdev = "e1000"
i need store these lines in variable can modify them , re-input them later on. to so, i'm using line this:
$devices = get-content $vmxsource | where-object {($_ -match "^ethernet" -and $_ -match ".virtualdev")}
1. hoping use wildcard instead of multiple -match statements, couldn't figure out. something searching for ethernet*.virtualdev = "e1000". is possible? would preferable?
2. i'm confused best way use parentheses in situation. would below line more "correct"?
$devices = get-content $vmxsource | where-object {($_ -match "^ethernet") -and ($_ -match ".virtualdev")}
3. then, need pipe contents of file, excluding lines mentioned above, file. to so, attempted this:
get-content $vmxsource | where-object {$_ -notmatch "^ethernet" -and $_ -notmatch ".virtualdev"} | set-content $vmxdest
however, didn't work, excluded lines contained ethernet or lines contained .virtualdev. if changed line worked correctly:
get-content $vmxsource | where-object {$_ -notmatch "^ethernet" -or $_ -notmatch ".virtualdev"} | set-content $vmxdestme, seems backwards, since -and worked when using -match. can explain logic i'm missing here?
hi jpw.meta,
quite picture have got, getting me scared :o
as query can use wildcards , using "1000" would require have `"1000`" escape char in front.
try this:
get-content .\whrdata.txt | ?{$_ -like "*ethernet*.virtualdev*"}
ethernet0.virtualdev = "e1000"
ethernet1.virtualdev = "e1000"
ethernet2.virtualdev = "e1000"
whrdata.txt
ethernet0.virtualdev = "e1000"
ethernet1.virtualdev = "e2000"
ethernet1.virtualdev = "e1000"
tom
tommy
ethernet1.virtualdev = "e2000"
tom2
ethernet2.virtualdev = "e1000"
tom
ethernet1.virtualdev = "e2000"
tommy
tom2
get-content .\whrdata.txt | ?{$_ -like "*ethernet*.virtualdev = `"e2000`""}| out-file whrdataresult.txt
not matching:
get-content .\whrdata.txt | ?{$_ -notlike "*ethernet*.virtualdev*"}
rather running way better use if{}else{} want both data.
"" | out-file .\whrdataresult.txt
"" | out-file .\whrdataresultn.txtget-content .\whrdata.txt | %{if($_ -like "*ethernet*.virtualdev*"){$_ | out-file .\whrdataresult.txt -append}else{$_ | out-file .\whrdataresultn.txt -append}}
regards,
satyajit
please“vote as helpful” if find contribution useful or “markas answer” if answer question. encourage me - , others - take time out you.
Windows Server > Windows PowerShell
Comments
Post a Comment