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
Solved

Pass file as parameter to powershell script

Posted on 2014-12-02
2
636 Views
Last Modified: 2014-12-07
Is it possible to pass a file to a powershell script as parameter, so that I will run the script as below ? Tks

.\myshut.ps1  List1.csv

Import-CSV .\computer1.csv | ForEach-Object{
stop-computer -Computer $_.Name -Force -ErrorAction silentlycontinue}
0
Comment
Question by:AXISHK
2 Comments
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 40477946
Param(
	[Parameter(Mandatory=$True)]
	[string]$Path
)
If (-Not (Test-Path -Path $Path)) {
	"File '$Path' not found!" | Write-Error
	Exit 1
}
Import-CSV -Path $Path | ForEach-Object {
	Stop-Computer -Computer $_.Name -Force -ErrorAction SilentlyContinue
}

Open in new window

0
 

Author Closing Comment

by:AXISHK
ID: 40485979
Tks
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

860 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