Solved

PowerShell Script - While Do

Posted on 2014-12-24
3
282 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
  • 2
3 Comments
 
LVL 69

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 69

Expert Comment

by:Qlemo
ID: 40517201
Oops.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
A procedure for exporting installed hotfix details of remote computers using powershell
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

11 Experts available now in Live!

Get 1:1 Help Now