This works (all on one line):
PS C:\> $list.item | % { $t = $_.tags -split ","; if ($t -contains "red") { $_.name } }
This, however, doesn't:
PS C:\> $list.item | % {
>> $t = $_.tags -split ","
>> if ($t -contains "red") { $_.ne }
>> }
>>
No matter what I enter, I just keep getting the ">>" prompt until I hit Ctrl-C.
What's wrong?
To get powershell to execute the command, you would in fact need to press enter twice.
Related
I found a little script to get all the local groups and members and it's working perfectly but I need to write the output on PowerShell.
Trap {"Error: $_"; Break;}
function EnumLocalGroup($LocalGroup) {
$Group = [ADSI]"WinNT://$strComputer/$LocalGroup,group"
"`r`n" + "Group: $LocalGroup"
$Members = #($Group.psbase.Invoke("Members"))
foreach ($Member In $Members) {
$Name = $Member.GetType().InvokeMember("Name", 'GetProperty', $Null, $Member, $Null)
$Name
}
}
$strComputer = gc env:computername
"Computer: $strComputer"
$computer = [adsi]"WinNT://$strComputer"
$objCount = ($computer.PSBase.Children | Measure-Object).Count
$i = 0
foreach ($adsiObj in $computer.PSBase.Children) {
switch -regex ($adsiObj.PSBase.SchemaClassName) {
"group" {
$group = $adsiObj.Name
EnumLocalGroup $group
}
}
$i++
}
I already tried this:
function EnumLocalGroup($LocalGroup) | Out-File -FilePath "E:\PS\Malik\group.txt"
But the code won't start if I do that. I also tried to use this whole Out-File line at the end of the code after the } but doesn't work either and this is the only solution I find on Internet.
If you want to incorporate logging into a function you need to put it into the function body, e.g.
function EnumLocalGroup($LocalGroup) {
....
$foo = 'something'
$foo # output returned by function
$foo | Add-Content 'log.txt' # output to log file
...
}
or
function EnumLocalGroup($LocalGroup) {
...
$foo = 'something'
$foo | Tee-Object 'log.txt' -Append # output goes to log file and StdOut
...
}
Otherwise you have to do the logging when you call the function:
EnumLocalGroup $group | Add-Content 'C:\log.txt'
I have this script:
$params = '/r', '/f',
'/t', '0',
'/d', 'p:0:0',
'/c', 'PlannedRestart'
$servers | ForEach-Object {
$output = & shutdown.exe /m "\\${_}" #params 2>&1
if ($LastExitCode -eq 0) {
"{0}`tRestarted" -f $_
} else {
"{0}`tRestart failed:`t{1}" -f $_, $output
}
} | Set-Content '.\RestartServers_LOG.txt'
it prints like this when it fails:
server1 Restart failed:server1:error
i want it to print:
server1 Restart failed:error
If the hostname always appears at the beginning of the captured output you can remove it with a simple replacement, e.g. like this:
"{0}`tRestart failed:`t{1}" -f $_, ($output -replace "^${_}:")
If it can appear at different locations in the output string you need to provide a more complete output example.
I Googled a lot and someone said ` (backtick) can be used for line continuation but it does not work for me. I tried following but both would not work.
PS C:\> foreach ($i in 1,2) { echo $i; }
1
2
PS C:\> foreach ($i in 1,2) {
>> echo $i;
>> }
>> <== It's still waiting for input. Killed with Ctrl-C.
PS C:\> foreach ($i in 1,2) { `
>> echo $i; `
>> }
>> <== It's still waiting for input. Killed with Ctrl-C.
PS C:\>
You don't need to use backticks (or i didnt have to)
but you do need to press enter a second time.
PS C:\> foreach ($i in 1,2) { echo $i; }
1
2
PS C:\> foreach ($i in 1,2) {
>> echo $i;
>> }
>> <== Press enter again here.
1
2
PS C:\>
I presume this then lets the interpreter know that command has finished and allows it to execute.
I want to compare the number of files in subfolders of 2 given folders with a PowerShell script, but something isn't working.
I got the paths of the 2 folders through a user input and saved them
into the variables $where1 and $where2, I also saved the location where the script started of into the variable $a. I change to the folders given to get information about the subfolders in there into 2 arrays, one assioated array $folderArrayX with the numbers like this $folderArrayX["subfolder1"]=x (x being the number of child items for subfolder1) and one just with the names of the subfolders for easier comparision. The Code for the first folder given:
cd $where1;
$folderArray1 = #();
$folderArray1Keys = #();
Get-Childitem | Select-Object | ForEach-Object {
if ($_.PSIsContainer) {
$ArrayInArray = #{};
$folderArray1Keys += $_.Name.Trim();
$ArrayInArray[$_.Name.Trim()] = (Get-ChildItem ./$_).count;
$folderArray1 += $ArrayInArray;
}
}
I do the same for $where2 so I get $folderArray2 and $folderArray2Keys.
Now to compare these 2:
$r=() #something to save the returns
Compare-Object $folderArray1Keys $folderArray2Keys -IncludeEqual | ForEach-Object {
if ($_.SideIndicator -eq "=>") {
$r += ""+$_.InputObect+" only "+$where2+" : "+$folderArray2[$_.InputObject]+" files";
} elseif ($_.SideIndicator -eq "<=") {
$r += ""+$_.InputObect+" only "+$where1+" : "+$folderArray1[$_.InputObject]+" files";
} else {
if ($folderArray1[$_.InputObject] -gt $folderArray2[$_.InputObject]) {
$dif = ($folderArray1[$_.InputObject]-$folderArray2[$_.InputObject])
$r += "on both sides"+$_.InputObject+" has "+$diff+" more files in "+$where1
} elseif ($folderArray2[$_.InputObject] -gt $folderArray1[$_.InputObject]) {
$dif = ($folderArray2[$_.InputObject]-$folderArray1[$_.InputObject])
$r += "on both sides"+$_.InputObject+" has "+$diff+" more files in "+$where2
} else {
$r += ""+$_.InputObject+" is equal on both sides";
}
}
}
#Output
$r
cd $a
Some how this code doesn't work well. It indicates subfolders as equal which are clearly not (only because they are there and completely ignore the numbers), don't spell out the onesided folders (what I understand the least (it does so for the bothsided folders)) and don't show any numbers. I can't figure out the mistake because assiocated arrays work with PowerShell, I mean a code like this:
$k1="key1";
$v1=1;
$k2="key2";
$v2="value2";
$array=#{};
$array[$k1]=$v1;
$array[$k2]=$v2;
$array[$k1]
$array[$k2]
pause
# Exert
# 1
# value2
# Press any key to continue ...
works as expected.
Your comparison routine treats $folderArray1 and $folderArray2 as if they were hashtables, but you define them as arrays of hashtables. Basically you're doing this:
PS C:\> $h = #{}
PS C:\> $h['foo'] = 42
PS C:\> $a = #()
PS C:\> $a += $h
PS C:\> $a['foo']
PS C:\> $a[0]['foo']
42
when you actually want just this:
PS C:\> $a = #{}
PS C:\> $a['foo'] = 42
PS C:\> $a['foo']
42
Change the code for populating the $folderArray variables to this:
$folderArray = #{}
Get-Childitem | ? { $_.PSIsContainer } | ForEach-Object {
$folderArray[$_.Name] = #(Get-ChildItem $_.FullName).Count
}
and the Compare-Object statement to this:
Compare-Object #($folderArray1.Keys) #($folderArray2.Keys) -IncludeEqual | ...
and the problem should disappear.
Is there any way to view the In words section of an event thru Powershell or thru the command prompt.
I have tried
psloglist -m 120 -s -x
Get-EventLog application | ft TimeGenerated,#{label="Data";expression={[char[]][int[]]$_.data -join ""}}
But none give the desired output.
I got a quick and dirty one, which works on my laptop:
Get-EventLog application | % {
$bytes = $_.Data
while ($bytes.Length % 4 -ne 0) {$bytes = $bytes + #(0)}
for ($i = 0; $i -lt $bytes.Length; $i += 4)
{
$curWord = [System.BitConverter]::ToUInt32($bytes, $i).ToString("X8")
Write-Output $curWord
}
Write-Output ""
}