I'm trying to separate by a string (;), results from this :
$MasterKeys = ($MasterKeys | Where {$_.Name -ne $Null -AND $_.SystemComponent -ne "1" -AND $_.ParentKeyName -eq $Null} | select-String Name,Version,ComputerName | sort Name| ft -hide )
Here is the output of this command:
HP ePrint SW 5.1.20088 LT00438
I would like this instead:
HP ePrint SW; 5.1.20088; LT00438
Well, this is the full code :
I don't know where to put the Export-Csv -Path "file.txt" -Delimiter ";" -NoTypeInformation function :(
Function Get-InstalledSoftware
{
Param
(
[Alias('Computer','ComputerName','HostName')]
[Parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Position=1)]
[string[]]$Name = $env:COMPUTERNAME
)
Begin
{
$LMkeys = "Software\Microsoft\Windows\CurrentVersion\Uninstall","SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
$LMtype = [Microsoft.Win32.RegistryHive]::LocalMachine
$CUkeys = "Software\Microsoft\Windows\CurrentVersion\Uninstall"
$CUtype = [Microsoft.Win32.RegistryHive]::CurrentUser
}
Process
{
ForEach($Computer in $Name)
{
$MasterKeys = #()
If(!(Test-Connection -ComputerName $Computer -count 1 -quiet))
{
Write-Error -Message "Unable to contact $Computer. Please verify its network connectivity and try again." -Category ObjectNotFound -TargetObject $Computer
Break
}
$CURegKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($CUtype,$computer)
$LMRegKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($LMtype,$computer)
ForEach($Key in $LMkeys)
{
$RegKey = $LMRegKey.OpenSubkey($key)
If($RegKey -ne $null)
{
ForEach($subName in $RegKey.getsubkeynames())
{
foreach($sub in $RegKey.opensubkey($subName))
{
$MasterKeys += (New-Object PSObject -Property #{
"ComputerName" = $Computer
"Name" = $sub.getvalue("displayname")
"SystemComponent" = $sub.getvalue("systemcomponent")
"ParentKeyName" = $sub.getvalue("parentkeyname")
"Version" = $sub.getvalue("DisplayVersion")
})
}
}
}
}
ForEach($Key in $CUKeys)
{
$RegKey = $CURegKey.OpenSubkey($Key)
If($RegKey -ne $null)
{
ForEach($subName in $RegKey.getsubkeynames())
{
foreach($sub in $RegKey.opensubkey($subName))
{
$MasterKeys += (New-Object PSObject -Property #{
"ComputerName" = $Computer
"Name" = $sub.getvalue("displayname")
"SystemComponent" = $sub.getvalue("systemcomponent")
"ParentKeyName" = $sub.getvalue("parentkeyname")
"Version" = $sub.getvalue("DisplayVersion")
})
}
}
}
}
$files = "inventaireLT.txt"
$MasterKeys = ($MasterKeys | Where {$_.Name -ne $Null -AND $_.SystemComponent -ne "1" -AND $_.ParentKeyName -eq $Null} | select Name,Version,ComputerName | sort Name| ft -hide )
$MasterKeys >> $files
}
}
End
{
}
}
Clear-Content -path $files
Import-Module ActiveDirectory
Get-ADComputer -LDAPfilter "(name=LT*)" -SearchBase "OU=S****Y,DC=T***R,DC=com" | Get-InstalledSoftware
(gc inventaireLT.txt) | ? {$_.trim() -ne "" } | set-content inventaireLT.txt
You can export your data to a semicolon separated file by using Export-Csv and specifying the -Delimiter ";"
$MasterKeys | Where {$_.Name -ne $Null -AND $_.SystemComponent -ne "1" -AND $_.ParentKeyName -eq $Null} | select Name,Version,ComputerName | Export-Csv -Path "C:\folder\file.txt" -Delimiter ";" -NoTypeInformation
If you don't want the header line:
$MasterKeys | Where {$_.Name -ne $Null -AND $_.SystemComponent -ne "1" -AND $_.ParentKeyName -eq $Null} |
select Name,Version,ComputerName |
ConvertTo-Csv -Delimiter ";" -NoTypeInformation |
Select-Object -Skip 1 |
Set-Content -Path "C:\folder\file.txt"
EDIT:
In your code you would replace:
$MasterKeys = ($MasterKeys | Where {$_.Name -ne $Null -AND $_.SystemComponent -ne "1" -AND $_.ParentKeyName -eq $Null} | select Name,Version,ComputerName | sort Name| ft -hide )
$MasterKeys >> $files
with
$MasterKeys | Where {$_.Name -ne $Null -AND $_.SystemComponent -ne "1" -AND $_.ParentKeyName -eq $Null} | select Name,Version,ComputerName | ConvertTo-Csv -Delimiter ";" -NoTypeInformation | Select-Object -Skip 1 | Set-Content -Path $files
Related
what I want is that the $report_groups val gives me in Export-CSV the output that i get in Terminal. But i cant figure it why does he gives me Numbers.
$get_AD_Groups = Get-ADGroup -Filter '*' | Select-Object Name
$report_groups = New-Object -TypeName System.Collections.ArrayList
foreach ($item in $get_AD_Groups) {
$get_users = $item.Name | Get-ADGroupMember | Select-Object Name
$disabled_user = 0
foreach ($user in $get_users) {
$status = $user.Name | Get-ADUser -ErrorAction SilentlyContinue
if(($status.ObjectClass -eq 'user') -and ($status.Enabled -ne 'True')) {
$disabled_user++
}
}
if ($get_users.Count -eq $disabled_user) {
$report_groups.Add($item.Name)
}
}
$report_groups | Export-Csv -Path "..\report.csv" -NoTypeInformation -Force -Delimiter ";"
Now when i run $report_groups in Terminal i get the list of the AD Group BUT as soon i do the Export-CSV this is what i get:
So thanks again to Lee_Dailey for helping me on this.
Changes done.
$report_groups = #()
if ($get_users.Count -eq $disabled_user) {
$fill = [PSCustomObject]#{
AD_GROUP = $item.Name
}
$report_groups += $fill
}
I am trying to print the variable whenever the variable $PrinterStatus is returning any data but the correct data is not coming with If else logic.
$CurrentTime = Get-Date
$PrinterStatus=
Get-Printer -ComputerName "TGHYT-6578UT" | Foreach-Object {
$Printer = $_
$Printer | Get-Printjob |
Where-Object {$_.jobstatus -ne "Normal" -and $_.SubmittedTime -le $CurrentTime.AddHours(-1) } |
Select-Object #{name="Printer Name";expression={$_.printerName}},
#{name="Submitted Time";expression={$_.SubmittedTime}},
jobstatus, #{name="Port";expression={$Printer.PortName}},
#{name="Document Name";expression={$_.documentname}},
#{n='Difference in Hours';e={[math]::Truncate(($CurrentTime - $_.SubmittedTime).TotalHours)}} |
Sort-Object -Property jobstatus -Descending
}
if([string]::IsNullOrEmpty($PrinterStatus))
{
Write-Output "Printers NOT Present"
$output = $PrinterStatus > "C:\Output.txt" #Shoud give blank txt file
}
else {
Write-Output "printers Present"
$output = $PrinterStatus > "C:\Output.txt"
}
As your $PrinterStatus will be an array of your custom print job objects, you can check the length of that array.
$CurrentTime = Get-Date
$PrinterStatus = #()
$PrinterStatus = Get-Printer -ComputerName "TGHYT-6578UT" | Foreach-Object {
Get-Printjob $_ |
Where-Object {$_.jobstatus -ne "Normal" -and $_.SubmittedTime -le $CurrentTime.AddHours(-1) } |
Select-Object #{name="Printer Name";expression={$_.printerName}}, #{name="Submitted Time";expression={$_.SubmittedTime}}, #{name="jobstatus";expression={$_.jobstatus}}, #{name="Port";expression={$Printer.PortName}}, #{name="Document Name";expression={$_.documentname}}, #{n='Difference in Hours';e={[math]::Truncate(($CurrentTime - $_.SubmittedTime).TotalHours)}} |
Sort-Object -Property jobstatus -Descending
}
if ($PrinterStatus.Count -eq 0) {
Write-Output "Printers NOT Present"
} else {
Write-Output "Printers Present"
}
$PrinterStatus > "C:\Output.txt"
I also cleaned up your code a little and fixed the insertion of jobstatus in your custom object.
I create an empty $array early in my script and later i loop over some stuff and want to add this stuff to my array. This works as intended.
$AspxFiles = gci $path $filter -r | ? { $_.DirectoryName -notlike '*Werkstatt*' -and `
$_.DirectoryName -notlike '*- Kopie*' -and `
$_.DirectoryName -notlike '*.1*' -and `
$_.DirectoryName -notlike '*.0*' -and `
$_.DirectoryName -notlike '*.2*' -and `
$_.Name -notlike '*- Kopie*'
}
$array = #()
foreach ($file in $AspxFiles)
{
$getURL = sls -Path $file.FullName -Pattern $regex -AllMatches | ? { $_ -notlike '*www.w3.org*' -and `
$_ -notlike '*jquery.com*' -and `
$_ -notlike '*www.mwe*' } |
% { $_.Matches } | % { $_.Value }
foreach ($URL in $getURL)
{
$Request = [System.Net.WebRequest]::Create($URL)
$Response = $Request.GetResponse()
$Status = [int]$Response.StatusCode
if ($Status -ne 200 -or $Response.ResponseUri -like '*PageNotFound*')
{
$x = [PSCustomObject] #{
File = $file.Name
URL = $URL
} ; $array += $x
}
$Response.Close()
}
}
But the output of $array is like this:
#{File=B-1.56.aspx; URL=http://sdfsdfsdf/b-1.39.4_fr1_d.pdf}
#{File=B-1.56.aspx; URL=http://sdfsdfsdfssd/b-1.39.4_fr1_d.pdf}
#{File=B-1.58.aspx; URL=https://sdfffssd/b-1.39.6_de_d.pdf}
#{File=B-1.58.aspx; URL=https://fsdfsfb-1.39.6_de_d.pdf}
How can I get this formatted like a list so I can grab the File and URL Property to send it as an E-Mail body like this?:
$body = $array | sort File | ConvertTo-Html -Property File,URL -Head "<html><h2>Folgende Links wurden im Katalog nicht gefunden:</h2><br></html>" | Out-String
Looks like this is still only a part of your script but:
I'd transform your anded wildcards into a -notmatch RegEx with OR'ed | entries
and set $array = Foreach($file ... gathering all ouput
$REDir = '.*Werkstatt.*|.*- Kopie.*|.*\.[012].*'
$REURL = '.*www\.w3\.org.*|.*jquery\.com.*|.*www.mwe.*'
$AspxFiles = Get-ChildItem $path $filter -r |
Where-Object { $_.DirectoryName -notmatch $REDir -and `
$_.Name -notlike '*- Kopie*'}
$array = #()
$array = foreach ($file in $AspxFiles) {
$getURL = Select-String -Path $file.FullName -Pattern $regex -AllMatches |
Where-Object { $_ -notmatch $REURL } |
ForEach-Object { $_.Matches } | ForEach-Obkect { $_.Value }
ForEach ($URL in $getURL) {
$Request = [System.Net.WebRequest]::Create($URL)
$Response = $Request.GetResponse()
$Status = [int]$Response.StatusCode
if ($Status -ne 200 -or $Response.ResponseUri -like '*PageNotFound*') {
[PSCustomObject] #{
File = $file.Name
URL = $URL
}
}
$Response.Close()
}
}
$body = $array | Sort-Object File |
ConvertTo-Html -Property File,URL -Head `
"<html><h2>Folgende Links wurden im Katalog nicht gefunden:</h2><br></html>" |
Out-String
I have a script containing a function Create-RebootData containing child functions such as Full and Full has a child function named Generate-RebootData where the output variable $Global:result is created.
Within Full there are multiple Where-Object two statements to filter the $Global:result into by date and time. Example below.
Is there an easier method to accomplish this instead of the multiple Where-Object statements?
The desired result are
Set-StrictMode -Version 1.0
Function Create-RebootData{
[CmdletBinding(SupportsShouldProcess=$true,DefaultParameterSetName="ViewOnly")]
Param(
[Parameter(ParameterSetName="ViewOnly")]
[Switch]$ViewOnly,
[Parameter(ParameterSetName="Full")]
[Switch]$Full,
)
Switch ($PSCmdlet.ParameterSetName){
"ViewOnly"
{
ViewOnly
}
"Full"
{
Full
}
}#end switch
Function Full{
Generate-RebootData
$Global:result | Where-Object {$_ -like '*fri?2:00*' -and $_.MaintenanceWindow `
-notmatch 'all.da.servers' -and $_.Server -match "^ITD"} | % {"{0}" -f $_.Server} | `
Out-File D:\Scripts\Full-Servers.txt -Append
$Global:result | Where-Object {$_ -like '*fri?2:00*' -and $_.MaintenanceWindow `
-notmatch 'all.da.servers' -and $_.Server -match "^ITD"} | `
% {"{0}" -f $_.MaintenanceWindow -replace `
"^NA.+", "$((get-date).AddDays(1).ToString('MM-dd-yy')) 01:50"} | `
Out-File D:\Scripts\Full-Times.txt -Append
}
Function Generate-RebootData{
IF(Get-Command Get-SCOMAlert -ErrorAction SilentlyContinue){}ELSE{Import-Module OperationsManager}
"Get Pend reboot servers from prod"
New-SCOMManagementGroupConnection -ComputerName Server01
$AlertData = Get-SCOMAlert -Criteria "MyString" | Select NetbiosComputerName
New-SCOMManagementGroupConnection -ComputerName Server02
$AlertData += Get-SCOMAlert -Criteria "MyString" | Select NetbiosComputerName
"Remove duplicates"
$AlertDataNoDupe = $AlertData | Sort NetbiosComputerName -Unique
"Create hash table"
$table = #{}
"Populate hash table"
$MaintenanceWindow = Import-Csv D:\Scripts\MaintenanceWindow2.csv
$MaintenanceWindow | ForEach-Object {$table[$_.Computername] = $_.'Collection Name'}
"Create final object"
$Global:result = #{}
"Begin Loop"
$Global:result = $AlertDataNoDupe | ForEach-Object { [PSCustomObject] #{
Server=$_.NetbiosComputerName
MaintenanceWindow= if($table.ContainsKey($_.NetbiosComputerName)){
$table[$_.NetbiosComputerName]
}Else { "Not Found!"}
PingCheck=IF(Test-Connection -Count 1 $_.NetbiosComputerName -Quiet -ErrorAction SilentlyContinue){"Alive"}
ELSE{"Dead"}
LastReboot=Try{$operatingSystem = Get-WmiObject Win32_OperatingSystem -ComputerName $_.NetbiosComputerName -ErrorAction Stop
[Management.ManagementDateTimeConverter]::ToDateTime($operatingSystem.LastBootUpTime)}
Catch{"Access Denied!"}
} }
}
You can do it like this:
$Global:result | Where-Object {
$_ -like '*fri?2:00*' -and
$_.MaintenanceWindow -notmatch 'all.da.servers' -and
$_.Server -match '^ITD'
} | ForEach-Object {
'{0}' -f $_.Server | Out-File D:\Scripts\Full-Servers.txt -Append
'{0}' -f $_.MaintenanceWindow -replace '^NA.+',
'{0} 01:50' -f (Get-Date).AddDays(1).ToString('MM-dd-yy') | Out-File D:\Scripts\Full-Times.txt -Append
}
But I agree with Mathias' comment, this function probably should be refactored.
I have a script that works great but I would like to only export the machines that meet one of the three conditions in the foreach statement. Right now it exports all the machines, which I have to clean up manually in excel.
#Create an LDAP searcher object and pass in the DN of the domain we wish to query
$Searcher = New-Object System.DirectoryServices.DirectorySearcher([ADSI]"LDAP://DC=ten,DC=thomsonreuters,DC=com")
#Pass in the ceriteria we are searching for.
#In this case we're looking for computers that are enabled and running Windows 7
$Searcher.Filter = "(&(objectCategory=computer)(objectClass=computer)(!UserAccountControl:1.2.840.113556.1.4.803:=2)(operatingSystem=Windows 7*))"
$Searcher.PageSize = 100000
# Populate General Sheet(1) with information
$results = $Searcher.Findall()
$results | ForEach-Object { $_.GetDirectoryEntry() } |
select #{ n = 'CN'; e = { ($_.CN) } },
#{ n = 'DistinguishedName'; e = { $_.DistinguishedName } },
#{ n = 'extensionattribute7'; e = { $_.extensionattribute7 } },
#{ n = 'extensionattribute1'; e = { $_.extensionattribute1 } },
#{ n = 'NewComputerName'; e = { 'Filler' } } |
Export-Csv 'C:\temp\Windows7_Only.csv' -NoType -Force
$csv = Import-Csv -Path "c:\Temp\Windows7_Only.csv"
foreach ($row in $csv)
{
if (($row.CN -notmatch '^U\d{7}') -and ($row.DistinguishedName -like "*Laptops*") -and ($row.extensionattribute7 -match '^U\d{7}$') -and ($row.CN -notmatch '\d{3}'))
{
$row.NewComputerName = $row.extensionattribute7 + "-TPL-ZZ"
}
elseif (($row.CN -notmatch '^U\d{7}') -and ($row.DistinguishedName -like "*Desktops*") -and ($row.extensionattribute7 -match '^U\d{7}$') -and ($row.CN -notmatch '\d{3}'))
{
$row.NewComputerName = $row.extensionattribute7 + "-TPD-ZZ"
}
elseif (($row.CN -notmatch '^U\d{7}') -and ($row.DistinguishedName -like "*Virtual*") -and ($row.extensionattribute7 -match '^U\d{7}$') -and ($row.CN -notmatch '\d{3}'))
{
$row.NewComputerName = $row.extensionattribute7 + "-TPV-ZZ"
}
}
$csv | export-csv c:\temp\fixed.csv -NoTypeInformation -Force
Try this:
$csv = $csv | where { ($_.CN -notmatch '^U\d{7}') -and ($_.CN -notmatch '\d{3}') -and ($_.extensionattribute7 -match '^U\d{7}$') -and (($_.DistinguishedName -like "*Laptops*") -or ($_.DistinguishedName -like "*Desktops*") -or ($_.DistinguishedName -like "*Virtual*")) }
Before the final line. It should filter out any ones that haven't met any of the criteria in the foreach loop