Solved

PowerShell Script - While Do

Posted on 2014-12-24
3
312 Views
Last Modified: 2014-12-24
Hi,

I am pretty new with Powershell.  I have a script that run a SCCM report and export it to CSV that works fine.  Is there a way to optimize that script to run 15 SCCM reports and export to CSV file?  Kind a "While Do" function?

Here below the script I use:

#Set the variables

$xhttp = $nul
$stream = $nul
$username = "xxxxxxxxxx"
$password = get-content C:\ExportCSV\cred.txt | convertto-securestring
$url = 'http://999.999.999.999/SMSReporting_CS1/Report.asp?ReportID=774'
$destination = "\\Pack01\HC01-all.csv"

#Start XML
$xhttp = new-object -com msxml2.xmlhttp
$xhttp.open("Post",$url,$false,$username,$password)
$xhttp.setrequestheader("Content-Type","application/x-www-form-urlencoded")

#this sends the request to perform a CSV export
$xhttp.send("export=yes")

#This line's not needed but I use it to get confirmation
#that the download is queued correctly.  If not OK then
#I know to check the above part of this script
$xhttp.statustext

#Now grab the file in the buffer and save it to disk
$stream = new-object -com ADODB.Stream
$stream.open()
$stream.type = 1

#Connect the buffer to the downloaded file
$stream.write($xhttp.responsebody)
$stream.savetofile($destination,2)
$stream.close()
$stream = $nul


Thanks for your help
:)
0
Comment
Question by:mldaigle1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40516417
The best way to do that is to write the core code as a function you can call with different parameters.
functiong get-SCCMReport([String] $reportID, [String] $destination)
{

  $username = "xxxxxxxxxx"
  $password = get-content C:\ExportCSV\cred.txt | convertto-securestring
  $url = "http://999.999.999.999/SMSReporting_CS1/Report.asp?ReportID=$reportID"

  #Start XML
  $xhttp = new-object -com msxml2.xmlhttp
  $xhttp.open("Post",$url,$false,$username,$password)
  $xhttp.setrequestheader("Content-Type","application/x-www-form-urlencoded")

  #this sends the request to perform a CSV export
  $xhttp.send("export=yes")

  #This line's not needed but I use it to get confirmation
  #that the download is queued correctly.  If not OK then
  #I know to check the above part of this script
  Write-Host "$url - result: $($xhttp.statustext)"

  #Now grab the file in the buffer and save it to disk
  $stream = new-object -com ADODB.Stream
  $stream.open()
  $stream.type = 1

  #Connect the buffer to the downloaded file
  $stream.write($xhttp.responsebody)
  $stream.savetofile($destination,2)
  $stream.close()
  $stream = $nul

}

get-SCCMReport 774 \\Pack01\HC01-all.csv
get-SCCMReport 775 \\Pack01\HC01-all-but-one.csv

Open in new window

0
 

Author Closing Comment

by:mldaigle1
ID: 40516485
This is exactly what I wanted!!  
You are my Christmas gift!
:)


(I just needed to remove the letter "g" at the end of "function" in the code and it work perfectly)
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40517201
Oops.
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

729 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