how to get parameters from switch or file [duplicate]












0
















This question already has an answer here:




  • How do I write a PowerShell script that accepts pipeline input?

    4 answers




I've seen in the past before I started scripting that a powershell script that requires a hostname parameter. it could either by typed in like Script.PS1 "hostname"
but you could also run the script in the following method:



gc hostnames.txt | script.ps1


and in this way you could list a number of hostnames in your text file.



how is this achieved.
my parameters looks as follows



Param(
[Parameter(Mandatory=$True)]
[ValidateSet("C","K")]
[ValidateNotNullorEmpty()]
[String]$Action,

[Parameter(Mandatory = $True)]
[ValidateNotNullorEmpty()]
[String]$Computers

)









share|improve this question













marked as duplicate by LotPings, Matt powershell
Users with the  powershell badge can single-handedly close powershell questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 14:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • The question asked is not about getting parameters from the pipeline although the syntax submitted implies that. The suggested duplicate doesn't address this.

    – No Refunds No Returns
    Nov 23 '18 at 15:35


















0
















This question already has an answer here:




  • How do I write a PowerShell script that accepts pipeline input?

    4 answers




I've seen in the past before I started scripting that a powershell script that requires a hostname parameter. it could either by typed in like Script.PS1 "hostname"
but you could also run the script in the following method:



gc hostnames.txt | script.ps1


and in this way you could list a number of hostnames in your text file.



how is this achieved.
my parameters looks as follows



Param(
[Parameter(Mandatory=$True)]
[ValidateSet("C","K")]
[ValidateNotNullorEmpty()]
[String]$Action,

[Parameter(Mandatory = $True)]
[ValidateNotNullorEmpty()]
[String]$Computers

)









share|improve this question













marked as duplicate by LotPings, Matt powershell
Users with the  powershell badge can single-handedly close powershell questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 14:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • The question asked is not about getting parameters from the pipeline although the syntax submitted implies that. The suggested duplicate doesn't address this.

    – No Refunds No Returns
    Nov 23 '18 at 15:35
















0












0








0









This question already has an answer here:




  • How do I write a PowerShell script that accepts pipeline input?

    4 answers




I've seen in the past before I started scripting that a powershell script that requires a hostname parameter. it could either by typed in like Script.PS1 "hostname"
but you could also run the script in the following method:



gc hostnames.txt | script.ps1


and in this way you could list a number of hostnames in your text file.



how is this achieved.
my parameters looks as follows



Param(
[Parameter(Mandatory=$True)]
[ValidateSet("C","K")]
[ValidateNotNullorEmpty()]
[String]$Action,

[Parameter(Mandatory = $True)]
[ValidateNotNullorEmpty()]
[String]$Computers

)









share|improve this question















This question already has an answer here:




  • How do I write a PowerShell script that accepts pipeline input?

    4 answers




I've seen in the past before I started scripting that a powershell script that requires a hostname parameter. it could either by typed in like Script.PS1 "hostname"
but you could also run the script in the following method:



gc hostnames.txt | script.ps1


and in this way you could list a number of hostnames in your text file.



how is this achieved.
my parameters looks as follows



Param(
[Parameter(Mandatory=$True)]
[ValidateSet("C","K")]
[ValidateNotNullorEmpty()]
[String]$Action,

[Parameter(Mandatory = $True)]
[ValidateNotNullorEmpty()]
[String]$Computers

)




This question already has an answer here:




  • How do I write a PowerShell script that accepts pipeline input?

    4 answers








powershell parameters






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 14:34









Cleadus FetusCleadus Fetus

465




465




marked as duplicate by LotPings, Matt powershell
Users with the  powershell badge can single-handedly close powershell questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 14:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by LotPings, Matt powershell
Users with the  powershell badge can single-handedly close powershell questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 14:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • The question asked is not about getting parameters from the pipeline although the syntax submitted implies that. The suggested duplicate doesn't address this.

    – No Refunds No Returns
    Nov 23 '18 at 15:35





















  • The question asked is not about getting parameters from the pipeline although the syntax submitted implies that. The suggested duplicate doesn't address this.

    – No Refunds No Returns
    Nov 23 '18 at 15:35



















The question asked is not about getting parameters from the pipeline although the syntax submitted implies that. The suggested duplicate doesn't address this.

– No Refunds No Returns
Nov 23 '18 at 15:35







The question asked is not about getting parameters from the pipeline although the syntax submitted implies that. The suggested duplicate doesn't address this.

– No Refunds No Returns
Nov 23 '18 at 15:35














1 Answer
1






active

oldest

votes


















1














As asked you could use syntax something like



gc hostnames.txt | % {  .script.ps1 -Action A -Computers $_ }


and it would call script.ps1 once for each line in hostnames.txt.



The $_ is a placeholder for the current value of the array being iterated by the ... | % ... syntax.






share|improve this answer
























  • Correct answer. I'd like to add that I personally prefer to make scripts readable by not using 'gc' but Get-Content and not using '%' but ForEach-Object or ForEach. This makes it a lot easier to understand and especially for the ones without a lot of experience.

    – Bernard Moeskops
    Nov 23 '18 at 15:21






  • 1





    Old habits die hard for lazy hackers

    – No Refunds No Returns
    Nov 23 '18 at 15:33


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














As asked you could use syntax something like



gc hostnames.txt | % {  .script.ps1 -Action A -Computers $_ }


and it would call script.ps1 once for each line in hostnames.txt.



The $_ is a placeholder for the current value of the array being iterated by the ... | % ... syntax.






share|improve this answer
























  • Correct answer. I'd like to add that I personally prefer to make scripts readable by not using 'gc' but Get-Content and not using '%' but ForEach-Object or ForEach. This makes it a lot easier to understand and especially for the ones without a lot of experience.

    – Bernard Moeskops
    Nov 23 '18 at 15:21






  • 1





    Old habits die hard for lazy hackers

    – No Refunds No Returns
    Nov 23 '18 at 15:33
















1














As asked you could use syntax something like



gc hostnames.txt | % {  .script.ps1 -Action A -Computers $_ }


and it would call script.ps1 once for each line in hostnames.txt.



The $_ is a placeholder for the current value of the array being iterated by the ... | % ... syntax.






share|improve this answer
























  • Correct answer. I'd like to add that I personally prefer to make scripts readable by not using 'gc' but Get-Content and not using '%' but ForEach-Object or ForEach. This makes it a lot easier to understand and especially for the ones without a lot of experience.

    – Bernard Moeskops
    Nov 23 '18 at 15:21






  • 1





    Old habits die hard for lazy hackers

    – No Refunds No Returns
    Nov 23 '18 at 15:33














1












1








1







As asked you could use syntax something like



gc hostnames.txt | % {  .script.ps1 -Action A -Computers $_ }


and it would call script.ps1 once for each line in hostnames.txt.



The $_ is a placeholder for the current value of the array being iterated by the ... | % ... syntax.






share|improve this answer













As asked you could use syntax something like



gc hostnames.txt | % {  .script.ps1 -Action A -Computers $_ }


and it would call script.ps1 once for each line in hostnames.txt.



The $_ is a placeholder for the current value of the array being iterated by the ... | % ... syntax.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 14:52









No Refunds No ReturnsNo Refunds No Returns

5,48441936




5,48441936













  • Correct answer. I'd like to add that I personally prefer to make scripts readable by not using 'gc' but Get-Content and not using '%' but ForEach-Object or ForEach. This makes it a lot easier to understand and especially for the ones without a lot of experience.

    – Bernard Moeskops
    Nov 23 '18 at 15:21






  • 1





    Old habits die hard for lazy hackers

    – No Refunds No Returns
    Nov 23 '18 at 15:33



















  • Correct answer. I'd like to add that I personally prefer to make scripts readable by not using 'gc' but Get-Content and not using '%' but ForEach-Object or ForEach. This makes it a lot easier to understand and especially for the ones without a lot of experience.

    – Bernard Moeskops
    Nov 23 '18 at 15:21






  • 1





    Old habits die hard for lazy hackers

    – No Refunds No Returns
    Nov 23 '18 at 15:33

















Correct answer. I'd like to add that I personally prefer to make scripts readable by not using 'gc' but Get-Content and not using '%' but ForEach-Object or ForEach. This makes it a lot easier to understand and especially for the ones without a lot of experience.

– Bernard Moeskops
Nov 23 '18 at 15:21





Correct answer. I'd like to add that I personally prefer to make scripts readable by not using 'gc' but Get-Content and not using '%' but ForEach-Object or ForEach. This makes it a lot easier to understand and especially for the ones without a lot of experience.

– Bernard Moeskops
Nov 23 '18 at 15:21




1




1





Old habits die hard for lazy hackers

– No Refunds No Returns
Nov 23 '18 at 15:33





Old habits die hard for lazy hackers

– No Refunds No Returns
Nov 23 '18 at 15:33





Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga