What does this Powershell command mean in English? - powershell

In Exchange Management Shell, you can write a query to display administrator activities.
Search-AdminAuditLog -Startdate ((get-date).AddDays(-5)) -EndDate (get-date) | where{$_.caller -ne "NT AUTHORITY\SYSTEM (MSExchangeHMWorker)"} |select Caller, Rundate, ObjectModified, CmdLetName, #{n="Parameters"; e={$e=$null;$_.CmdLetParameters|%{$e += ( " -" + $_.name.tostring() + " '" + $_.value + "'")};$e}} | ft -autosize
Most of the commands make sense but can anyone explain the #... part in English? Specifically this part:
#{n="Parameters"; e={$e=$null; $_.CmdLetParameters | %{$e += ( " -" + $_.name.tostring() + " '" + $_.value + "'")};$e}}

That's called a "Calculated property". It's a way to add a property in your selected object with the name specified by n= and having the value resulting from expression e=.
http://technet.microsoft.com/en-us/library/ff730948.aspx

Related

ForEach-Object $_ Variable not working as expected

I am trying to get the real names of each user in an AD group, I have created it to the point of getting the output to show the User IDs of each member in the group but then trying to do a net user $_ /domain on each user in the .txt file it gives me errors like there is nothing there. Before the onslaught of "why dont you just use Get-AD*" I understand that its out there, but we cannot install that module here. I need something other people can use without installing things.
I have tried a few ways to approach this and the below code is the part of the script that doesnt seem to be working correctly. I have the other way I tried it commented out but left it there as another possible starting point.
function Return-DropDown {
$Choice = $DropDown.SelectedItem.ToString()
if ($Choice)
{
net group $Choice /domain > C:\Temp\RawOut.txt
$UserID = Get-Content 'C:\Temp\RawOut.txt'
$UserID[($UserID.IndexOf('-------------------------------------------------------------------------------') + 1) .. ($UserID.IndexOf('The command completed successfully.') -1)] > C:\Temp\RawIDs.txt
Start C:\Temp\RawIDs.txt
Remove-Item C:\Temp\RawOut.txt
Get-Content -path C:\Temp\RawIDs.txt | ForEach-Object {net user $_ /domain | findstr "Full Name"} >> C:\Temp\$Choice+RealNames.txt
#$RealNames = Get-Content -path C:\Temp\RawIDs.txt
#ForEach ($_ in $RealNames)
#{
# net user $_ /domain >> C:\Temp\$Choice+1.txt
# }
}
}
What I am getting back is:
net : The syntax of this command is:
At C:\Users\MyID\OneDrive - MyCompany\AD Group DropDown.ps1:34 char:64
+ ... path C:\Temp\RawIDs.txt | ForEach-Object {net user $_ /domain} >> C:\ ...
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (The syntax of this command is::String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
NET USER
[username [password | *] [options]] [/DOMAIN]
username {password | *} /ADD [options] [/DOMAIN]
username [/DELETE] [/DOMAIN]
username [/TIMES:{times | ALL}]
username [/ACTIVE: {YES | NO}]
Let me know any advice for getting this function to work.
Thank you to #LotPings for helping me out with finding a solution.
I discovered (through looking at a post LotPings commented) that I was getting the output in a format that was causing an error. My txt file was formatted:
User1 User2 User3
User4 User5 User6
...
And it was trying to take the full line User1 User2 User3 as the variable. Which was obviously not working. with using a -replace and -split to get the format right for my variable and it's working now.
function Return-DropDown {
$Choice = $DropDown.SelectedItem.ToString()
if ($Choice)
{
net group $Choice /domain > C:\Temp\RawOut.txt
$UserID = Get-Content 'C:\Temp\RawOut.txt'
$UserID[($UserID.IndexOf('-------------------------------------------------------------------------------') + 1) .. ($UserID.IndexOf('The command completed successfully.') -1)] > C:\Temp\RawIDs.txt
#Start C:\Temp\RawIDs.txt
Remove-Item C:\Temp\RawOut.txt
$results = Get-Content -path C:\Temp\RawIDs.txt
foreach($result in $results) {
($result -replace '\s+',',') -split ',' | ? { $_ } >> 'C:\temp\users.txt'
}
Get-Content -path C:\Temp\users.txt | ForEach-Object {net user $_ /domain | findstr "Full Name"} >> C:\Temp\$Choice.txt
Remove-Item C:\Temp\users.txt
Remove-Item C:\Temp\RawIDs.txt
Start C:\Temp\$Choice.txt
}
}

List all azure resources with tagnames and tagvalues using PowerShell

I am trying to fetch OperatingHours tag details for all the Azure VirtualMachines and Azure SqlDatabases.
Following are the possibility for appID in a resource and the values I need to print in output correspondingly:
If OperatingHours tag itself is not present in any resource then display "Tag not present"
if OperatingHours tag is present but contains null or empty string then display "NULL/EMPTY"
if OperatingHours tag is present with any other value then display that value.
Do I need to take care of option (2) separately or is it like printing any normal value of the OperatingHours.
After long efforts I have created following script:
$ErrorOccured = $false
$resources = Get-AzureRmResource |
Where-Object {($_.ResourceType -eq "Microsoft.Compute/virtualMachines") -or ($_.ResourceType -eq "Microsoft.Sql/servers/databases")} |
foreach {
new-object psobject -Property #{
ResourceName = $_.ResourceName;
ResourceType = $_.ResourceType;
OperatingHours= try {
$ErrorActionPreference = 'SilentlyContinue';
($_ | select -expand Tags).OperatingHours; }
catch {
$ErrorOccured = $true ; }
if ($ErrorOccured)
{ "Tag not present" }
else {
($_ | select -expand Tags).OperatingHours;
$ErrorOccured = $false };}
}
$resources | Format-Table
When running this script, I am receiving following error:
At line:13 char:58
+ }
+ ~
The hash literal was incomplete.
At line:20 char:2
+ }
+ ~
Unexpected token '}' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : IncompleteHashLiteral
If I replace the OperatingHours statement with following code then the script is running with success. But in doing so, I am not able to satisfy the option (1) mentioned above.
Operating Hours = if (!($_ | select -expand Tags).OperatingHours)
{"Tag not present"}
else {($_ | select -expand Tags).OperatingHours} ;
Please advise me on how to correct this and get the required output.
Thanks
After alot of hit and trial and rigorous testing, I found what I was looking for:
OperatingHours = if ( ($_ | select -expand Tags).OperatingHours -ieq $null )
{"TAG NOT PRESENT"}
elseif ( ($_ | select -expand Tags).OperatingHours -ieq '')
{"NULL/EMPTY"}
else
{($_ | select -expand Tags).OperatingHours } ;
I replaced the OperatingHours statement in original script to above.
The solution looks easy and after finding it I was like how could I miss that earlier, but that's what learning process is all about, right ?

Errors in listing AD groups for AD PCs

I am writing a script for work and trying to determine why my code is showing errors. I am new to this coding and want to understand what is wrong.
The errors I get are from the tag .... PC listings in my .txt file.
Ex: Get-Content : Cannot find path 'F:\tag 77909' because it does not exist.
My confusion is that when I write-host after the .Replace code it prints correctly
Ex:You cannot call a method on a null-valued expression. + $Notags =$PC.Replace <<<< ("tag ", "PC")
+ CategoryInfo : InvalidOperation: (Replace:String) [], RuntimeEx
ception
+ FullyQualifiedErrorId : InvokeMethodOnNull
Last error I get is that it only prints out the last PC.... ID in my .txt file listing??? I am unsure why given I have a foreach loop
**MY CODE SO FAR:**
Import-Module activedirectory
$compImports = Get-Content "C:\Temp\temp\input.txt"
$groupExport = "C:\temp\temp\output.txt"
Clear-Content $groupExport
$Header = "PC Name" + "|" + "Group Name" + "|" + "Group Description"
#Write header
$Header | Out-File $groupExport -Append
#get PC tag listing
$PCs = Get-Content $compImports
#For loop to change all "tag " to "PC"
foreach($PC in $PCS)
{
$Notags =$PC.Replace("tag ", "PC")
}
#loop to get information and print it out
foreach ($Notag in $Notags) {
$computerobj = Get-ADComputer $Notag -Properties memberof
$computerobj.memberof | ? {$_ -match '^CN=APP.*'} `
| % {get-adgroup $_ -Properties name, description} | `
% {$computerobj.Name + "|" + $_.name + "|" + $_.description `
| Out-File $groupExport -Append}
}
I see at least one issue here
$compImports = Get-Content "C:\Temp\temp\input.txt"
...
$PCs = Get-Content $compImports
You are calling Get-Content twice which would generate the error you are seeing most likely.
Could be simplified as
$PCs = Get-Content "C:\Temp\temp\input.txt"
Your other error should go away as a result since $PCs should contain real data at that point.

Displaying Active directory extended properties

The following script is using the Active Directory extended properties to filter the results correctly as it only shows AD users where "script_ignore" is in the 'info' field (this is the 'Notes' field on the Telephones tab in AD users & computers).
However it doesn't display any extended properties as I'd expected in the following foreach-object %_info or $_city.
How can I output extended properties?
get-aduser -filter { info -like "script_ignore" } | % {
$_.name + " " + $_.city + " " + $_.info
}
You need to add any property you're interested in which are not included in the default property set, in the Properties parameter.
get-aduser -filter { info -like "script_ignore" } -Properties City,Info | % {
$_.name + " " + $_.city + " " + $_.info
}

Powershell filter files by SQL Server result set and copy

I am executing a query in SQL Server and returning a single column result set. I need to loop through the result set and find file names matching the record from the result set. Part of the file name is a sequence number. I need to sort the files in ascending order, select the first file, and then copy only that file to a subdirectory. The records in the result set look like this:
MEMBERS.net MEMBERS_COMMENTS1.net
MEMBERS_COMMENTS2.net
MEMBERS_LANGUAGE.net
MEMBERS_COVER_OHC.net
MEMBERS_PROBLEM_LIST.net
The file names have this kind of structure:
00_1914330_MEMBERS.net
Can someone tell me why this does not achieve my end result?
add-pssnapin sqlservercmdletsnapin100
add-pssnapin sqlserverprovidersnapin100
cd SQLSERVER:\SQL\LOCALHOST\DEFAULT\Databases\SYSDB\Tables\dbo.STG0_EXPORT_JOBS
$ds = Invoke-Sqlcmd -ServerInstance 'LOCALHOST' -Query "SELECT CASE WHEN SUBSTRING([EXPORT_NAME],1,3) = 'MHC' THEN SUBSTRING([EXPORT_NAME],5,(LEN([EXPORT_NAME])))+'.net' ELSE [EXPORT_NAME]+'.net' END AS export FROM [SYSDB].[dbo].[STG0_EXPORT_JOBS] WHERE [JOB_NAME] = 'MHC_STG0_MEMBERS'"
foreach ($files in $ds) {
$oldestfile = Get-ChildItem C:\Scripts |
where{!$_.PSIsContainer -and $_.Name.EndsWith("$($files.export)")} |
sort-object -property name | select-object -first 1 Name |
Copy-Item "C:\Scripts\" + $oldestfile.substring(7,$oldestfile.length - 8) `
C:\Scripts\MEMBERS
}
Here is what I get when I run this:
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
PS H:> C:\powershell_MoveKoreFiles.ps1
Unexpected token 'in' in expression or statement.
At C:\powershell_MoveKoreFiles.ps1:1 char:472
+ add-pssnapin sqlserverprovidersnapin100 add-pssnapin sqlservercmdletsnapin100
Set-Location SQLSERVER:\SQL\LOCALHOST\DEFAULT\Databases\SYSDB\Tables\dbo.STG0_
EXPORT_JOBS $ds=Invoke-Sqlcmd -Query "SELECT CASE WHEN SUBSTRING([EXPORT_NAME],
1,3) = 'MHC' THEN SUBSTRING([EXPORT_NAME],5,(LEN([EXPORT_NAME])))+'.net' ELSE [
EXPORT_NAME]+'.net' END AS export FROM [SYSDB].[dbo].[STG0_EXPORT_JOBS] WHERE [
JOB_NAME] = 'MHC_STG0_MEMBERS'" -ServerInstance "LOCALHOST" foreach ($files in
<<<< $ds){$oldestfile = Get-ChildItem C:\Scripts|where{!$.PSIsContainer -and
$.Name.EndsWith("$($files.export)")}|sort-object -property name -descending|se
lect-object -first 1 Name|Copy-Item -path "C:\Scripts\"+$oldestfile.substring(7
,$oldestfile.length - 8) -destination C:\Scripts\MEMBERS}
+ CategoryInfo : ParserError: (in:String) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
PS H:>
I think there might be one too many pipes in the script. Remove the one after the Select -first 1 Name e.g.:
Add-PSSnapin sqlservercmdletsnapin100
Add-PSSnapin sqlserverprovidersnapin100
cd SQLSERVER:\SQL\LOCALHOST\DEFAULT\Databases\SYSDB\Tables\dbo.STG0_EXPORT_JOBS
$ds = Invoke-Sqlcmd -ServerInstance 'LOCALHOST' -Query "SELECT CASE WHEN " +
"SUBSTRING([EXPORT_NAME],1,3) = 'MHC' THEN " +
"SUBSTRING([EXPORT_NAME],5,(LEN([EXPORT_NAME])))+'.net' " +
"ELSE [EXPORT_NAME]+'.net' END AS export " +
"FROM [SYSDB].[dbo].[STG0_EXPORT_JOBS] " +
"WHERE [JOB_NAME] = 'MHC_STG0_MEMBERS'"
foreach ($files in $ds)
{
$oldestfile = Get-ChildItem C:\Scripts |
Where {!$_.PSIsContainer -and $_.Name.EndsWith("$($files.export)")} |
Sort name | Select Name -First 1
$oldName = "C:\Scripts\$($oldestfile.substring(7,$oldestfile.length - 8))"
Copy-Item $oldName C:\Scripts\MEMBERS
}