Solved

PowerShell Script - While Do

Posted on 2014-12-24
3
278 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 68

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 68

Expert Comment

by:Qlemo
ID: 40517201
Oops.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

18 Experts available now in Live!

Get 1:1 Help Now