Reputation: 5709
I am not new to Powershell, but I am new to making scripts in Powershell myself.
I had to make a simple script which takes all of our employees at work and a bit of info about them, and then output a CSV file with this information. But when I run the code below, nothing happens. The ISE simply executes the process, a second passes and it's done. Then no CSV file is created.
PROCESS {
$path = Split-Path -parent "C:\Users\omitted\Desktop"
$pathexist = Test-Path -Path $path
If($pathexist -eq $false) {
New-IOItem -type directory -Path $path
}
$csvreportfile = $path + "\ALLADUsers_.csv"
#import the AD Module
Import-Module ActiveDirectory
Get-ADUser -SearchBase "OU=Medarbejdere,OU=Users,OU=omitted,DC=omitted,DC=local" -Properties MemberOf -Filter * |
Select-Object @{ Label = "First Name"; Expression = { $_.GivenName } },
@{ Label = "Last Name"; Expression = { $_.Surname } },
@{ Label = "Display Name"; Expression = { $_.DisplayName } },
@{ Label = "Logon Name"; Expression = { $_.SamAccountName } },
@{ Label = "Job Title"; Expression = { $_.Title } },
@{ Label = "Description"; Expression = { $_.Description } },
@{ Label = "Department"; Expression = { $_.Department } } |
#Export CSV Report
Export-Csv -Path $csvreportfile -NoTypeInformation
}
What did I do wrong?
Upvotes: 0
Views: 50
Reputation: 24410
Try this:
#moved the Parent parameter to the end to more clearly distinguish Path's value from Parent (switch).
$path = Split-Path -Path "C:\Users\omitted\Desktop" -Parent
#Since we don't refer to $PathExist again, skipped the assignment and put the test directly in the condition
If(Test-Path -Path $path) {
#Corrected Type; was `New-IOItem`
New-Item -type directory -Path $path
}
#use Join-Path when joining paths, to avoid issues with too many/too few slashes
$csvreportfile = Join-Path $path "ALLADUsers_.csv"
#import the AD Module
Import-Module ActiveDirectory
#Added other properties which wouldn't be returned by default (see https://social.technet.microsoft.com/wiki/contents/articles/12037.active-directory-get-aduser-default-and-extended-properties.aspx for a list of all properties / those in Cyan are the only ones which don't need to be listed on the Properties parameter)
Get-ADUser -SearchBase "OU=Medarbejdere,OU=Users,OU=omitted,DC=omitted,DC=local" -Properties MemberOf, Title, Description, Department -Filter * |
Select-Object @{ Label = "First Name"; Expression = { $_.GivenName } },
@{ Label = "Last Name"; Expression = { $_.Surname } },
@{ Label = "Display Name"; Expression = { $_.DisplayName } },
@{ Label = "Logon Name"; Expression = { $_.SamAccountName } },
@{ Label = "Job Title"; Expression = { $_.Title } },
@{ Label = "Description"; Expression = { $_.Description } },
@{ Label = "Department"; Expression = { $_.Department } } `
| Export-Csv -Path $csvreportfile -NoTypeInformation #moved this so that the pipeline feeds into export-csv; the whitespace had made this statement invalid previously
I've added comments inline explaining the changes I've made.
Regarding the PROCESS
section, that's not needed if you're running this directly in the script's body. I'd normally include this if I were defining a function/cmdlet (see https://ss64.com/ps/syntax-function-input.html).
Upvotes: 1