I have last month excel sheet with following
Current month excel sheet with following
Now i would like find what is new server Name added in current month then list them out as shown below
So far i got following code. any idea would be appreciated ? i will be scheduling this with windows scheduler task .This should be powershell since i will be adding more code later how to pick right excel sheet from SMB share.
i am trying this
$oldbk = Import-Excel -Path '\\hcohesity05\cohesity_reports\2022\7\07-29\Cohesity_FETB_Report-2022-07-29-14-26-48.xlsx'
$newbk = Import-Excel -Path '\\hcohesity05\cohesity_reports\2022\8\08-26\Cohesity_FETB_Report-2022-08-26-14-26-48.xlsx'
$Compare = Compare-Object $oldbk $newbk -Property "Server Name" -includeDifferent -PassThru
$Compare | Export-Excel -Path '.\diff.xlsx'
but getting message
A parameter cannot be found that matches parameter name 'includeDifferent'.
By looking at your code I'm assuming you're only interested in finding the servers not present in the old Excel document, if that's the case you only need Where-Object for filtering:
$oldbk = Import-Excel -Path 'path\to\oldfile.xlsx'
Import-Excel -Path 'path\to\newfile.xlsx' | Where-Object {
$_."Server Name" -notin $oldbk."Server Name"
} | Export-Excel -Path 'path\to\diff.xlsx'
As for the error message, Compare-Object does not have a -IncludeDifferent parameter and shows differences between two objects by default.
$Compare = Compare-Object $oldbk $newbk -Property "Server Name" -PassThru
Related
I'm working on a project where I need to take a text file and make it an excel file. So far what I've come up with is this.
cls
Remove-Item -path D:\Users\zabin\OneDrive\Desktop\ITS3410\WEEK8\MainWarehouse.csv
Add-Content -path D:\Users\zabin\OneDrive\Desktop\ITS3410\WEEK8\MainWarehouse.csv -Value '"Part_Number","Cost","Price"'
$csvPath = 'D:\Users\zabin\OneDrive\Desktop\ITS3410\WEEK8\MainWarehouse.csv'
#region Excel Test
If (test-path HKLM:SOFTWARE\Classes\Word.Application) {
Write-host "Microsoft Excel installed"
} else {
Write-host "Microsoft Excel not installed"
}
#endregion
#region Patterns
$mainpattern1 = '(?<Partnumber>\d*\s*\w*,)(?<Cost>\d*.\d*),(?<Price>\d*.\d*)'
$mainpattern2 = '(?<Part_number>\d*-\d*-\d*),(?<Cost>\d*.\d*),(?<Price>\d*.\d*)'
#endregion
get-Content 'D:\Users\zabin\OneDrive\Desktop\ITS3410\WEEK8\MainWarehouse.csv' | #grabs the content
Select-String -Pattern $mainpattern1, $mainpattern2 | #selects the patterns
Foreach-Object {
$Part_Number, $Cost, $Price = $_.Matches[0].Groups['Part_number', 'Cost','Price']
[PSCustomObject] #{
part_number = $Part_Number
Cost = $Cost
Price = $Price
}
$objResults | Export-Csv -Path $csvPath -NoTypeInformation -Append
}
some sample data is here
00001143 SP,136.41,227.35
00001223 SP,48.66,81.10
00001236 SP,149.72,249.53
0001-0003-00,100.95,168.25
00015172 W,85.32,142.20
I'm getting the file created and the header values are correct but I'm not sure how to get the values to import.
Continuing from my comment... with the resources and a simple example.
Find-Module -Name '*excel*' | Format-Table -AutoSize
# Results
<#
Version Name Repository Description
------- ---- ---------- -----------
7.1.1 ImportExcel PSGallery PowerShell module to import/export Excel spreadsheets, without Excel....
0.1.12 PSWriteExcel PSGallery Little project to create Excel files without Microsoft Excel being installed.
1.0.2 PSExcel PSGallery Work with Excel without installing Excel
...
0.6.9 ExcelPSLib PSGallery Allow simple creation and manipulation of XLSX file
2.1 Read-ExcelFile PSGallery PowerShell module to import Excel spreadsheets, without Excel....
...
#>
MSExcel will read properly formatted CSV's natively. So, to convert to a true XLS file, use PowerShell, MSOffice COM to open MSExcel with the CSV file, then save it as an XLS format.
$FileName = "$env:temp\Report"
Get-Process |
Export-Csv -UseCulture -Path "$FileName.csv" -NoTypeInformation -Encoding UTF8
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$excel.Workbooks.Open("$FileName.csv").SaveAs("$FileName.xlsx",51)
$excel.Quit()
explorer.exe "/Select,$FileName.xlsx"
Your use case is of course as noted:
Import-Csv -Path 'D:\temp\book1.txt' -header Title, Author
Then using COM as noted above.
I ended up solving this after I asked the question I had alot of flaws in this code
Add-Content -path D:\Users\zabin\OneDrive\Desktop\ITS3410\WEEK8\MainWarehouse.csv -Value '"Part_Number","Cost","Price"'
$csvPath = 'D:\Users\zabin\OneDrive\Desktop\ITS3410\WEEK8\MainWarehouse.csv'
#region Excel Test
If (test-path HKLM:SOFTWARE\Classes\Excel.Application) {#these next few lines will check if excel is installed on the system
Write-host "Microsoft Excel installed"
} else {
Write-host "Microsoft Excel not installed"
}
#endregion
#region Patterns
$mainpattern1 = '(?<Part_number>\d*\s*\w*),(?<Cost>\d*.\d*),(?<Price>\d*.\d*)'#These two line will use REGEX to help seperate the data
$mainpattern2 = '(?<Part_number>\d*-\d*-\d*),(?<Cost>\d*.\d*),(?<Price>\d*.\d*)'
#endregion
get-Content 'D:\Users\zabin\OneDrive\Desktop\ITS3410\WEEK8\Main.rtf' | #grabs the content
Select-String -Pattern $mainpattern2, $mainpattern1 | #selects the patterns
Foreach-Object {
$Part_number, $Cost, $Price = $_.Matches[0].Groups['Part_number', 'Cost','Price'] #Gets the groups of a call to select-string
$results = [PSCustomObject] #{#the list here is what i use to seperate the data onto the CSV file
part_number = $Part_Number
Cost = $Cost
Price = $Price
}
$results | Export-Csv -Path $csvPath -NoTypeInformation -Append #moves the results to the CSV file
}
I have a folder of spreadsheets which all start with "WEEK COMM" and then the date. For example "WEEK COMM 24-12-2018". What I am looking to do is a script that finds the latest file, makes a copy of it in the SAME folder, and changes the date in the filename by adding 7 days.
So far I have got this, which successfully locates the most up to date file, and copies it as "WEEK COMM TEST", but I am unable to find anything about adding a date to the filename.
Get-ChildItem -Path "\\DESKTOP-88SIUP6\Users\User\Desktop\Shared\STOCK ORDERS\2018" |
Sort-Object -Property CreationTime -Descending |
Select-Object -First 1 |
Copy-Item -Destination "\\DESKTOP-88SIUP6\Users\User\Desktop\Shared\STOCK ORDERS\2018\WEEK COMM TEST.xlsx" -Force
Could anyone please help me?
this will capture the string date from the file .BaseName property, convert it to a [datetime] object, add 7 days, convert that to a date string in your [backwards [grin]] format, and then replace the old date string with the new date string in the .FullName of the file.
that should make the rename process pretty direct & easy. [grin]
if you can, you would likely have a rather easier time if you switched to the more logical, properly sortable yyyy-MM-dd format. you may not be able to do so, but it's worth trying ...
$FileName = [System.IO.FileInfo]'WEEK COMM 24-12-2018.xlsx'
$FN_StringDate = $FileName.BaseName.Split(' ')[2]
$FN_Date = [datetime]::ParseExact($FN_StringDate, 'dd-MM-yyyy', $Null)
$NewFN_StringDate = $FN_Date.AddDays(7).ToString('dd-MM-yyyy')
$NewFileName = $FileName.FullName -replace $FN_StringDate, $NewFN_StringDate
$FileName.FullName
$NewFileName
output [old, then new] ...
D:\Data\Scripts\WEEK COMM 24-12-2018.xlsx
D:\Data\Scripts\WEEK COMM 31-12-2018.xlsx
edit to add a very specific example. it's untested since i haven't any such location ... that is why the -WhatIf is there. [grin]
$SourceDir = '\\DESKTOP-88SIUP6\Users\User\Desktop\Shared\STOCK ORDERS\2018'
Get-ChildItem -Path $SourceDir |
Sort-Object -Property CreationTime -Descending |
Select-Object -First 1 |
ForEach-Object {
$FN_StringDate = $_.BaseName.Split(' ')[2]
$FN_Date = [datetime]::ParseExact($FN_StringDate, 'dd-MM-yyyy', $Null)
$NewFN_StringDate = $FN_Date.AddDays(7).ToString('dd-MM-yyyy')
$NewFileName = $_.FullName -replace $FN_StringDate, $NewFN_StringDate
# remove the "-WhatIf" when you are ready to do this for real
Copy-Item -LiteralPath $_.FullName -Destination $NewFileName -WhatIf
}
I am working on a script for PowerShell to import data from 2 separate Excel sheets, compare all properties of these sheets and export a new Excel worksheet with this output.
The purpose of this script is to compare an updated excel sheet with a previous version and spot what has changed, what has been added and possibly even what has been removed.
I have been using the Import-Excel module to handle the first part, and Compare-Object for the data comparison.
As far as I can tell Import-Excel imports the Excel data into a System.Object which is a hashtable of PSCustomObjects. Each PSCustomObject corresponds to a row in the Excel sheet.
I am running the following code:
$global:InputdataA = Import-Excel -Path $InputA -WorkSheetname $InputSheetA
$global:InputdataB = Import-Excel -Path $InputB -WorkSheetname $InputSheetB
$global:ReferenceObject = $InputdataA
$global:DifferenceObject = $InputdataB
$global:InputdataHeadersA = $InputdataA[0].psobject.properties.name
$global:InputdataHeadersB = $InputdataB[0].psobject.properties.name
$props = $InputdataHeadersA
$props += $InputdataHeadersB
$props = $props | Select -Unique
$compareResult = Compare-Object -ReferenceObject $ReferenceObject -DifferenceObject $DifferenceObject -Property $props -PassThru -CaseSensitive
I am using 2 excel files for testing:
Personnel_16_12_2018 - small2.xlsx (previous version)
Personnel_28_11_2018 - small2.xlsx (new version with changes and additions)
The files can be downloaded here:
https://ufile.io/bmstu
https://ufile.io/3z62x
The output I would like to see would be only the 7 entries that have been changed/added, with data from difference object (the new version of the excel sheet) This would represent the latest and "correct" data.
Currently I get an output from the compare-object, containing the 7 entries AND the 6 entries that have been changed from the reference object including side-indicator.
Is it possible to make the compare-object return only the changes or do I have to process the output afterwards?
Based on your additional information and the sample files, you could try something like this:
$oldFile = Import-Excel ".\personnel_28_11_2018---small2.xlsx"
$newFile = Import-Excel ".\personnel_16_12_2018---small2.xlsx"
$properties = "TRIAL_PK", "TRIALCOUNTRY_PK", "TRIALSSITE_PK", "ASSIGNMENT_LVL", "ROLE", "INT_EXT", "START_DATA", "END_DATE", "PERSONNELL_PK", "TITLE", "LAST_NAME", "FIRST_NAME", "ORGANIZATION_NAME"
$result = Compare-Object -ReferenceObject $oldFile -DifferenceObject $newFile -Property $properties -PassThru -CaseSensitive | Where-Object {$_.SideIndicator -eq "=>"}
$result | Select-Object $properties | Export-Excel ".\changed.xlsx"
I am creating spreadsheet to compare the hotfixes on servers that I am also using for historical record to help diagnose issues between servers.
I have downloaded Douglas Fink's module that was featured by The Scripting Guys which will allow me to export-excel and it works very well.
I am writing the data to a csv, and then sorting it as I write it out to excel workbook. This works well, but I would like to also add a subtotal based on the count of KBxxx so that if I have the total number that matches my servers, I know that all of my servers have that KB.
I am just starting with Powershell, and have never worked with writing excel. Please help or give me some guidance.
$Date = Get-Date
$files = Get-ChildItem -path $PSScriptRoot\servers\*.*
$Filename = "-" + $Date.Year + "-" + $Date.Month + "-" + $Date.Day + "_" + $date.Hour + $date.Minute
foreach ($server in $servers){
$server
Get-hotfix -ComputerName $server |
Select CSName, Description,HotFixID,InstalledOn|
Export-Csv -Path "$PSScriptRoot\output\$($file.basename)-HotFix-list-$($Filename).csv" -Append -NoTypeInformation
}
Import-Csv -path "$PSScriptRoot\output\$($file.basename)-HotFix-list-$($Filename).csv" | Sort-Object -Property HotFixID |Export-Excel -Path "$PSScriptRoot\output\$($file.basename)-list-$($Filename).xlsx" -WorksheetName HotFixID -Append
#Remove-Item -Path "$PSScriptRoot\output\*.csv" -Force
If I use the subtotal command with the following settings, It totals the data by each hotfix.
I can easily see all servers queried have the top KB (1) When I expand the Hotfix selection (2) It shows me the remaining detail. (3)
I am learning by getting the script to run, and then adding a little more function each time.
Keith
hey im trying to build a powershell script that will pull a serial number from a remote pc and then match that against an xlsx file which would then match a column against another xlsx file i have gotten to the point where i can pull the remote sn and have everything put in to a csv output but i am having issues matching the data then filtering based on the match and then outputting only what i need im new to scripting so im pretty sure its more my lack of experiance than anything else this is my code so far
$computers = Get-Content c:\script\computerlist.txt
Get-wmiobject Win32_Bios -ComputerName $computers | Select-Object __SERVER, SerialNumber| Format-Table |out-file C:\script\computerinfo.txt
$computerinfo = Import-Excel C:\script\compDB.xlsx
$userinfo = Import-Excel C:\script\userDB.xlsx
$Computerinfo[2].SERIAL -eq
$Computerinfo[2].DATE_ADDED
$Computerinfo[2].OS
$Computerinfo[2].MODEL
$Computerinfo[2].USER
$userinfo[2].NAME_FIRST
$userinfo[2].NAME_LAST
$userinfo[2].NT_USERID
''
'Computer Info'
'----------'
$computerinfo ,$userinfo | Format-Table - | Out-File c:\script\computerinfo.csv
First you need to save the wmi information to a variable:
$WMIinfo = Get-wmiobject Win32_Bios -ComputerName $computers | Select-Object __SERVER, SerialNumber
Then you would need to loop through the spreadsheet and compare to data in Computer spreadsheet. If it matches loop through user spreadsheet for match:
foreach ($CompEntry in $Computerinfo) {
if ($WMIinfo.serial -eq $CompEntry.serial) {
foreach ($UserEntry in $userinfo) {
if ($UserEntry.NT_USERID -eq $CompEntry.USER) {
#output information you want here
}
}
}
I'll try to help you get there.
I created an Excel file called Serial.xslx. Here's what it looks like
SerialNumber DeployedTo
212 Ham
4M24N32 Stephen
I then import this as $list.
$list = import-excel C:\temp\serial.xlsx
Next,to get the Win32_Bios info, so I can grab the SerialNumber property.
$bios = get-WmiObject Win32_Bios
Finally, I'll filter through the $list (which contains the Excel file), and find a row which has a SerialNumber that matches this computers serial number. If I find a matching one, then I grab the .DeployedTo value for that record.
$user = $list | Where SerialNumber -eq $bios.SerialNumber | Select -ExpandProperty DeployedTo
All that remains is to demonstrate that it works.
"the computer with serial $($bios.SerialNumber) is deployed to $user"
>the computer with serial 4M24N32 is deployed to Stephen
Now, you've got two separate excel files, so I would either manually join them into one, or repeat this same basic approach.