Hello I hope you guys can help me with a problem that is bugging me for a couple of days now. I cannot get the output right when I export the results of the script to csv file I get the following.
function Get-ScheduledTask { [CmdletBinding()] param( [Parameter( Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [String[]]$ComputerName, [Parameter(Mandatory=$false)] [String[]]$RunAsUser, [Parameter(Mandatory=$false)] [String[]]$TaskName, [parameter(Mandatory=$false)] [alias("WS")] [switch]$WithSpace ) Begin { $Script:Tasks = @() } Process { $schtask = schtasks.exe /query /s $ComputerName /V /FO CSV | ConvertFrom-Csv Write-Verbose "Getting scheduled Tasks from: $ComputerName" if ($schtask) { foreach ($sch in $schtask) { if ($sch."Run As User" -match "$($RunAsUser)" -and $sch.TaskName -match "$($TaskName)") { Write-Verbose "$Computername ($sch.TaskName).replace('\','') $sch.'Run As User'" $sch | Get-Member -MemberType Properties | ForEach -Begin {$hash=@{}} -Process { If ($WithSpace) { ($hash.($_.Name)) = $sch.($_.Name) } Else { ($hash.($($_.Name).replace(" ",""))) = $sch.($_.Name) } } -End { $script:Tasks += (New-Object -TypeName PSObject -Property $hash) } } } } } End { $Script:Tasks } } $ComputerName = "SE94ABH02" $ServiceAccounts = Get-Content "D:\Scripts\Test-Peter\Testing\ServiceAccounts.txt" $obj = New-Object –TypeName PSObject $obj | Add-Member -MemberType NoteProperty -Name ServerName -Value $ComputerName $obj | Add-Member -MemberType NoteProperty -Name TaskName -Value ([string]::Join(",",(@()))) $obj | Add-Member -MemberType NoteProperty -Name ScheduledTaskState -Value ([string]::Join(",",(@()))) $obj | Add-Member -MemberType NoteProperty -Name LogonMode -Value ([string]::Join(",",(@()))) $obj | Add-Member -MemberType NoteProperty -Name Author -Value ([string]::Join(",",(@()))) $obj | Add-Member -MemberType NoteProperty -Name RunAsUser -Value ([string]::Join(",",(@()))) $obj | Add-Member -MemberType NoteProperty -Name ServiceName -Value ([string]::Join(",",(@()))) $obj | Add-Member -MemberType NoteProperty -Name StartName -Value ([string]::Join(",",(@()))) $SCHTSk = Get-ScheduledTask $ComputerName | Where-Object {$_.RunAsUser -like "NLKVKF94*"} | Select TaskName,ScheduledTaskState,LogonMode,Author,RunAsUser If ($SCHTSK) { $TEMP = @() foreach ($TskItem in $SCHTSK) { If ($TskItem -match "NLKVKF94") { $TEMP += $TskItem $info = @{ 'TaskName'=$TEMP.TaskName; 'ScheduledTaskState'=$TEMP.ScheduledTaskState; 'LogonMode'=$TEMP.LogonMode; 'Author'=$TEMP.Author; 'RunAsUser'=$TEMP.RunAsUser } } } $tskobj = New-Object -TypeName PSObject -Property $info $obj.TaskName += $tskobj.TaskName $obj.ScheduledTaskState += $tskobj.ScheduledTaskState $obj.LogonMode += $tskobj.LogonMode $obj.Author += $tskobj.Author $obj.RunAsUser += $tskobj.RunAsUser } $WmiObjectResultaat = Get-WmiObject -Class win32_service -computer $ComputerName | select-object __SERVER,Name,StartName If ($WmiObjectResultaat) { $TEMP = @() foreach ($item in $WmiObjectResultaat) { if ($ServiceAccounts -contains $Item.StartName) { $TEMP += $Item $info = @{ 'Name'=$TEMP.Name; 'Startname'=$TEMP.Startname } } } $Srvobj = New-Object -TypeName PSObject -Property $info $obj.ServiceName += $Srvobj.Name $obj.StartName += $Srvobj.Startname } $obj | Export-Csv -Path "D:\Scripts\Test-Peter\Testing\lalala.csv" -NoTypeInformation
ScheduledTasks
module that ships with Windows. – Bacon Bits