Solved

PowerShell Script - While Do

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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