[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

PowerShell Script - While Do

Posted on 2014-12-24
3
Medium Priority
?
332 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 71

Accepted Solution

by:
Qlemo earned 2000 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 71

Expert Comment

by:Qlemo
ID: 40517201
Oops.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

656 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