I am trying to create a backup powershell script for user documents. I have created a script where I am putting on a form box the username I want to backup and then the script proceeds. The last thing is that I want to have a rule that if I put a wrong name, the script will not proceed.
Does anyone knows how I can "read" the present useraccounts on a laptop, in order to create rule to cross-check the input with the useraccounts?
Best regards.
This will read all of the folders in C:\Users and attempt to find a corresponding account in AD:
Get-Childitem C:\users -Directory |
ForEach-Object {
$profilePath = $_.FullName
Get-AdUser -Filter {SamAccountName -eq $_.Name} |
ForEach-Object {
New-Object -TypeName PsCustomObject |
Add-Member -MemberType NoteProperty -Name Name -Value $_.Name -PassThru |
Add-Member -MemberType NoteProperty -Name ProfilePath -Value $profilePath -PassThru |
Add-Member -MemberType NoteProperty -Name SID -Value $_.SID -PassThru
}
} | Format-Table Name,SID, profilePath -AutoSize
Obviously you can modify to get different AD properties, if needed. Also, remove the Format-Table command in order to pipe the output objects for further manipulation.
Note:
This requires the AD PowerShell Module to be installed on the system you run the script on.
It only outputs an object if it finds a user account, so you won't see anything for folders it finds that have no account.
Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'" | Select Name
will give you names of local accounts. Other available fields for select are listed here.
Related
Get-NetTCPConnection | Select-Object -Property LocalAddress,LocalPort,RemoteAddress,RemotePort,State,#{name='NameofApp';expression={(Get-Process -id $_.OwningProcess).MainModule.FileVersionInfo.FileDescription}} | Format-Table -AutoSize
The above one works perfectly, Here I want to include the Username as well:
I know that Get-Process -IncludeUserName will return the UserName but I don't know how to join this in the above working command.
Using your current query, here is a simple modified approach that will solve your problem:
Get-NetTCPConnection | ForEach-Object {
$process = Get-Process -Id $_.OwningProcess -IncludeUserName
$description = $process.MainModule.FileVersionInfo.FileDescription
$username = $process.UserName
$properties = $_ | Select-Object -Property LocalAddress,LocalPort,RemoteAddress,RemotePort,State
$properties | Add-Member -Name "NameOfApp" -Type NoteProperty -Value $description
$properties | Add-Member -Name "UserName" -Type NoteProperty -Value $username
$properties
} | Format-Table -AutoSize
Explanation:
Pipe Get-NetTCPConnection through to Foreach-Object.
Get the process object with Get-Process, making sure to include User Names with the -IncludeUserName switch.
Store the descriptions and usernames in separate variables. Not necessary, but I split them up for clarity.
Get all the TCP connection properties that can be selected immediately with Select-Object into a System.Management.Automation.PSCustomObject. This uses the default pipeline variable $_ from Foreach-Object, which is basically the current item from GetNetTCPConnection. You can run Get-Help -Name about_Automatic_Variables to find out more about pipeline variables, and Get-Help -Name about_pipelines for finding out more about pipelines. Unfortunately, these Help files don't contain online versions.
Add the NameOfApp and UserName members to the object with Add-Member.
Format into a table with Format-Table and auto size columns with the -AutoSize switch.
You could also use regular foreach enumeration as well:
& {
foreach ($connection in Get-NetTCPConnection) {
$process = Get-Process -Id $connection.OwningProcess -IncludeUserName
$description = $process.MainModule.FileVersionInfo.FileDescription
$username = $process.UserName
$properties = $connection | Select-Object -Property LocalAddress,LocalPort,RemoteAddress,RemotePort,State
$properties | Add-Member -Name "NameOfApp" -Type NoteProperty -Value $description
$properties | Add-Member -Name "UserName" -Type NoteProperty -Value $username
$properties
}
} | Format-Table -AutoSize
The above is wrapping the foreach loop inside a script block, so you need to use the call operator & to run it. You can read more About Operators and About Script Blocks.
I'm having some trouble copying data from 1 CSV and pasting it into a template of another one.
The template has specific column names.
and the csv file I have with the data, I'm able to get each column, but I am having trouble pasting it into a the template.
I'm trying to copy the following data from 1 csv to the template and here are the columns
email --> internal_customer_ID
givenname --> first_name
surname --> last_name
mail --> email_address
mobilephone --> mobile_phone_1
officePhone --> landline_phone_1
Here is my current code.
#Clear Screen
CLS
#The path we are working with (use the path where we execute this script from)
$global:path = Split-Path $script:MyInvocation.MyCommand.Path
$DataFile = $path + "\dataFile.csv"
$ExportedFileCSV = $path + "\PopulatedTemplate.csv"
#Export the data file with our user info
Get-ADGroupMember -Identity SomeGroup | Get-ADUser -Properties * | Select-Object -Property GivenName, SurName, Mail, MobilePhone, OfficePhone | Export-Csv -path $DataFile -NoTypeInformation -Force
$dataInput = Import-Csv $DataFile
$dataOutput = Import-Csv $ExportedFileCSV
$dataInput | ForEach-Object {
$newData = $_
$dataOutput |
Add-Member -MemberType NoteProperty -Name "internal_customer_ID" -Value $newData.Mail -PassThru -Force|
Add-Member -MemberType NoteProperty -Name "landline_phone_1" -Value $newData.OfficePhone -PassThru -Force|
Add-Member -MemberType NoteProperty -Name "email_address" -Value $newData.Mail -PassThru -Force|
Add-Member -MemberType NoteProperty -Name "mobile_phone_1" -Value $newData.MobilePhone -PassThru -Force|
Add-Member -MemberType NoteProperty -Name "last_name" -Value $newData.SurName -PassThru -Force|
Add-Member -MemberType NoteProperty -Name "first_name" -Value $newData.GivenName -PassThru -force
} | Export-CSV $ExportedFileCSV
If I can avoid exporting the datafile in the first place and just appending the result from the
Get-ADGroupMember -Identity SomeGroup | Get-ADUser -Properties * | Select-Object -Property GivenName, SurName, Mail, MobilePhone, OfficePhone
Straight to the csv template, that would work for my needs too, I just wasn't sure how to do that.
Your line reading $dataOutput | Add-Member ... is the problem, I think. Add-Member is for adding an attribute to a single object, but $dataOutput at this point is a collection of objects. I think the interpreter thinks you're trying add a member attribute to an object array.
Try creating a new object for each output record, then do an Export-CSV -append onto your output CSV file.
I think something like this should work:
$dataInput | ForEach-Object {
$newData = $_
$newRecordProperties = [ordered]#{
"internal_customer_ID"=$newData.Mail
"landline_phone_1" = $newData.OfficePhone
"email_address" = $newData.Mail
"mobile_phone_1" = $newData.MobilePhone
"last_name" = $newData.SurName
"first_name" = $newData.GivenName
}
$newRecord = new-object psobject -Property $newRecordProperties
Write-Output $newRecord
} | Export-CSV $ExportedFileCSV -Append
As long as the columns names in the output CSV are the same as your new record object, I think it should be okay. I am not sure what happens if the columns in $ExportedFileCSV are in a different order than the $newRecord being exported, so I added [ordered] to the hash table. You may want to test this yourself.
For the second part of your question, pipe-lining the whole thing, something like this is probably what you're after:
Get-ADGroupMember -Identity SomeGroup |
Get-ADUser -Properties * |
Select-Object -Property #(
#{label="internal_customer_ID"; expression={$_.Mail}}
#{label="email_address"; expression={$_.Mail}}
#{label="landline_phone_1"; expression={$_.OfficePhone}}
#{label="first_name"; expression={$_.GivenName}}
#{label="last_name"; expression={$_.SurName}}
#{label="mobile_phone_1"; expression={$_.MobilePhone}}
) |
Export-Csv $ExportedFileCSV -Append
Select-Object above creates a custom object with the attribute name and attribute value matching label and the result of expression. Again, re-order to match the order the CSV columns should be in.
I'm attempting to create a custom PowerShell object using data from the WebAdministration module for IIS7. The object should capture the ClientIPAddress and SiteID from the GetExecuteRequest WmiMethod for Worker Processes in IIS.
$p = Get-WmiObject WorkerProcess -Namespace root\WebAdministration -ComputerName . | Invoke-WmiMethod -Name GetExecutingRequests | Select-Object -ExpandProperty OutputElement
$wpob = #()
foreach($w in $p)
{
$webrequests = New-Object PSObject
$webrequests | Add-Member -Type NoteProperty -Name ClientIP -Value ($w | select -ExpandProperty ClientIPAddress)
$webrequests | Add-Member -Type NoteProperty -Name SiteID -Value ($W | select -ExpandProperty SiteID)
$wpobj += $webrequests
}
The Problem:
Object ($wpobj) contains the 'ClientIP' and 'SiteID' under get-member, but you cannot get those values by using $wpobj.clientip or $wpobj.siteid. Only way I could retrieve those values were to use $wpobj | select clientip or $wpobj | select siteid
Not being able to get the values for the properties in the above mentioned way makes it more difficult to sort/group the data as I can't select the values later on down the pipeline.
Anyone know what I may be missing? I have not seen this before. Only in this particular case
Solution was to upgrade Powershell to V4.0. IE.invoke won't work on earlier versions of Powershell.
How can i add the server name at the left of each line result on this script?. Thank you!
$servers = Get-Content -path .\Machines.txt
[pscustomobject]$result = #()
$subresult =
ForEach ($server in $servers)
{
Set-Service -computername $servers -Name sacsvr -StartupType Disabled -PassThru
}
$result = $subresult
$result | Out-File local_group_members.csv
This is an example result:
Status Name DisplayName
------ ---- -----------
Stopped sacsvr Special Administration Console Helper
Stopped sacsvr Special Administration Console Helper
Stopped sacsvr Special Administration Console Helper
Alternatively you can just add a property to the objects you're outputting right now. Pipe your Set-Service to Add-Member like this:
Set-Service -computername $servers -Name sacsvr -StartupType Disabled -PassThru | Add-Member -MemberType NoteProperty -Name 'Server' -Value $Server -PassThru
Now each object that you pass to $subresult has a new property Server that is the name of the server it was run on. You'll probably want to pipe through Select when outputting to have the order you want.
$SubResult | Select Server, Status, Name, DisplayName | Export-CSV 'local_group_members.csv' -NoType
You can arbitrarily re-order or add to your output with Select-Object. You can use hash tables to include calculated properties such as your desired ServerName.
So for each server, you can set the services and tag the output with that server name:
ForEach ($server in $servers)
{
Set-Service -computername $server -Name sacsvr -StartupType Disabled -PassThru |
Select #{Name = 'ServerName'; Expression = {$server}}, Name, DisplayName, Status
}
The above is shorthand for:
Select-Object -Property (properties)
The -Property parameter allows you to select any arbitrary grouping of properties on the type of object being piped in. Another parameter, -InputObject allows us to pipe in objects by value.
I wrote this small script to pull the office property from get-user by piping the exchange mailbox object.
$server = "tms08"
$arrUser = get-mailbox -Server $server |Get-MailboxStatistics |select identity
foreach ( $id in $arrUuser)
{
$office = get-user -Identity $id.Identity |select office
$out += $id.Identity
}
$out
I don't get any output or red errors. just the warning:
WARNING:There is no data to return for
the specifed mailbox
'Globe/MobileElect Usertemplate',
because it has not been logged on to.
WARNING:By default only the first 1000
items are returned. To change the
number of items returned, specify the
parameter "-ResultSize". To return all
items specify "-ResultSize Unilimited"
(Note: REturning all items maytake a
long time and consume a large amount
of memory depending on the actual
number of items). It is not
recommended to store the results in a
variable; instead pipe the results to
another task or script to perform
batch changes.
Any ideas on what might be causing this?
My goal is to develop a script which executes once a day via scheduled task which compiles all mailbox names, mailbox sizes, totalitems, totaldeleted items, along with their office and description fields (from active directory).
I'm guessing the get-qaduser is part of the quest powershell addon. I'll install it locally and give it a try..
the identiy property seems to give a number similar to the guid which looks like
1234as01-4f54-1234-b1df-f1df1df12d2d
I tried running
get-user -identity 1234as01-4f54-1234-b1df-f1df1df12d2d
and it found a name (joey blasio) and recipient type (usermailbox)
then i ran
get-user -Identity 1234as01-4f54-1234-b1df-f1df1df12d2d | select displayname, distinguistedname
Displayname (Joey Blasio ) and DistinguishedName (CN=Joey
Blasio,OU=EWE,DC=BLA-1,DC=net)
It is done by DisplayName
$exchangeservers = Get-MailboxServer
$AllUsers = #()
$AllUsersEmail = #()
foreach ($server in $exchangeservers)
{
$AllUsers += Get-Mailbox -Server $server |Get-MailboxStatistics |select servername,displayname,itemcount,totalitemsize
}
foreach ($user in $AllUsers)
{
$obj = new-object psObject
$mailinfo = get-mailbox -identity $user.displayname |select PrimarySMTPAddress,Office, DistinguishedName
$tmp = [adsi]("LDAP://" + $mailinfo.DistinguishedName)
$obj |Add-Member -MemberType noteproperty -Name "Server Name" -Value $user.ServerName
$obj |Add-Member -MemberType noteproperty -Name "Display Name" -Value $user.DisplayName
$obj |Add-Member -MemberType noteproperty -Name "Item Count" -Value $user.ItemCount
$obj |Add-Member -MemberType noteproperty -Name "Total Item Size" -Value $user.TotalItemSize
$obj |Add-Member -MemberType noteproperty -Name "Email Address" -Value $mailinfo.PrimarySMTPAddress
$obj |Add-Member -MemberType noteproperty -Name "Office" -Value $mailinfo.Office
$obj |Add-Member -MemberType noteproperty -Name "Description" -Value $tmp.description
$AllUsersEmail += $obj
}
$AllUsersEmail |Export-Csv c:\test.csv -NoTypeInformation
I believe the problem is that you're accessing a mailbox that's never been accessed normally. Can you try this with a mailbox that you know the owner has opened and worked with? Or is that already the case?
Also, as I don't have access to my Exchange machine at the moment, can you give me an idea of what the Identity property contains? I'm absolutely certain using a cmdlet like Get-QADUser vs. Get-User in Exchange, will ultimately bring you more satisfaction. We just need to mesh up the right property from Get-MailboxStatistics to something Get-QADUser can consume, so that it can get you the right user.
It might also be a bit helpful to understand what your end goal is - possibly there's an entirely different approach that will get you to where you want to be.