Solved

Variable in Compare Object input in Powershell

Posted on 2013-12-23
4
423 Views
Last Modified: 2013-12-23
Please excuse my terminology as I'm a powershell/scripting novice, however I have a script developed that compares two csv files and provides differences between them in an output csv file.  I have another script that is used to execute the exchange powershell query to gather the date.  I have another question open on how to get those results in a date variable.  I would need to know how I can have the compare script, which I have included a piece of below, change the compare-objects each day it runs.

The data gather script is set to run once per day, dumping the results in the same folder location as this script.  I want to change the Export.csv and Exportold.csv to varibles based on date, so every time the script runs, it will change the variable of each of htese properties to one name being the current date, and comparing it to yesterday's date.

For example:

export.csv would be 20131223calpermissions.csv
exportold.csv would be 20131222calpermissions.csv

And this would automatically change each day, without having to modify the script.


compare-object (import-csv Export.CSV | sort) (import-csv Exportold.CSV | sort) -passthru -property Mailbox,User,'$_.AccessRights',IsValid |

If there is a better way of doing this, i'm open to suggestions, and again I'm a script novice so bear with me.  Thank you in advance!
0
Comment
Question by:fireguy1125
  • 2
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39736833
For today's date..
"$(get-date -f yyyyMMdd)calpermissions.csv"

Open in new window

For yesterday's date..
"$("{0:yyyyMMdd}" -f (get-date).AddDays(-1))calpermissions.csv"

Open in new window

0
 
LVL 1

Author Comment

by:fireguy1125
ID: 39736846
Would this be the proper format for my script?

compare-object (import-csv "$(get-date -f yyyyMMdd)calpermissions.csv" | sort) (import-csv "$("{0:yyyyMMdd}" -f (get-date).AddDays(-1))calpermissions.csv" | sort) -passthru -property Mailbox,User,'$_.AccessRights',IsValid |
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39736853
Yes it should work if you are running the code from the folder where the file exist.. Else you need to add the path...

For example..
import-csv "C:\temp\$(get-date -f yyyyMMdd)calpermissions.csv"
0
 
LVL 1

Author Closing Comment

by:fireguy1125
ID: 39736855
Tested works great! Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now