[PowerShell] Commandline parameters doorgeven?

Pagina: 1
Acties:
  • 460 views sinds 30-01-2008
  • Reageer

  • SeatRider
  • Registratie: November 2003
  • Laatst online: 15-02 09:29

SeatRider

Hips don't lie

Topicstarter
Ik probeer via powershell een AD user van een mailbox te voorzien. Exchange 2007 biedt hiervoor ondersteuning middels het fenomenale powershell. Ik heb een script gemaakt (create.ps1) dat ik aanroep via powershell.exe ./create.ps1

Nu zou ik graag de usernaam als variabele daaruit halen, zodat ik powershell.exe ./create.ps1 seatrider kan doen.

Binnen batchfiles is het simpel, daar heb je middels %1 etc de beschikking over alle commandparameters, ik heb geen idee of en hoe dat via powershell ook kan. Ik heb me een ongeluk gezocht op internet, maar vind alleen dingen over parameters in cmdlets.

Wie weet raad?

[ Voor 8% gewijzigd door SeatRider op 07-03-2007 14:19 ]

Nederlands is makkelijker als je denkt


  • alt-92
  • Registratie: Maart 2000
  • Niet online

alt-92

ye olde farte

Jouw vraag is letterlijk terug te vinden op Technet :)

http://www.microsoft.com/...2/PowerShell/default.aspx

ik heb een 864 GB floppydrive! - certified prutser - the social skills of a thermonuclear device


  • SeatRider
  • Registratie: November 2003
  • Laatst online: 15-02 09:29

SeatRider

Hips don't lie

Topicstarter
Niet echt, daar staat dit:
[string]$c = Read-Host "Enter computer name"
$c = $c.Replace("\","")
Get-WMIObject Win32_OperatingSystem
–Property ServicePackMajorVersion
–Computer $c
...en wat dat doet is een gebruiker vragen om een waarde in te vullen. Ik wil dat de parameter door een extern proces is in te vullen zonder interaktie.

Nederlands is makkelijker als je denkt


  • zwippie
  • Registratie: Mei 2003
  • Niet online

zwippie

Electrons at work

Staat hier misschien wat nuttigs?

http://www.leeholmes.com/...tatementAndNewobject.aspx

How much can you compute with the "ultimate laptop" with 1 kg of mass and 1 liter of volume? Answer: not more than 10^51 operations per second on not more than 10^32 bits.


  • SeatRider
  • Registratie: November 2003
  • Laatst online: 15-02 09:29

SeatRider

Hips don't lie

Topicstarter
Nee dat was 'm ook niet. Ik heb nog even verder gezocht, ditmaal naar arguments ipv parameters, en toen vond ik het antwoord op deze site:

http://www.captainliteral...E2%80%93-running-scripts/

Het script create.ps kan middels $args[0] etc gebruik maken van alle parameters/arguments die in de aanroep van het script worden meegegeven. De aanroep powershell.exe ./create.ps1 seatrider werkt dan: $args[0] komt dan overeen met "seatrider".

Dus werkt vergelijkbaar met parameters in batchfiles, en identiek aan parameters in cmdlets. sim-pel |:(

[ Voor 3% gewijzigd door SeatRider op 07-03-2007 15:30 ]

Nederlands is makkelijker als je denkt


  • alt-92
  • Registratie: Maart 2000
  • Niet online

alt-92

ye olde farte

Zo'n voorbeeld stond dus letterlijk in de eerste link die ik je gaf ;)

ik heb een 864 GB floppydrive! - certified prutser - the social skills of a thermonuclear device


  • SeatRider
  • Registratie: November 2003
  • Laatst online: 15-02 09:29

SeatRider

Hips don't lie

Topicstarter
alt-92 schreef op woensdag 07 maart 2007 @ 22:53:
Zo'n voorbeeld stond dus letterlijk in de eerste link die ik je gaf ;)
Zou je mij dat willen aanwijzen? Ik zie het echt niet, het hele woord $args komt ook op die pagina niet voor ...

Nederlands is makkelijker als je denkt


  • alt-92
  • Registratie: Maart 2000
  • Niet online

alt-92

ye olde farte

Okee...
Readability and Reuse

Writing a single line of commands and parameters, however, doesn't help with readability. Windows PowerShell lets you break this into something more readable, which you can type directly into the shell without ever writing a script. Here's how it might look:
code:
1
2
3
4
5
6
PS C:\> $names = get-content "c:\computers.txt"
PS C:\> foreach ($name in $names) {
>> $name
>> gwmi Win32_OperatingSystem -prop ServicePackMajorVersion -comp $name
>> }
>>


This time, the contents of the file are stored in the variable $names. This example still uses foreach, but it isn't being input through a pipeline, so you have to tell it which collection of objects to loop through and what variable to store each object in—the part that says ($name in $names). Everything else is pretty much the same, and as soon as you hit Enter, the code is executed and the results displayed.

If you want to use this same code repeatedly, you can simply make a function out of it. Once again, type this directly into the shell:
code:
1
2
3
4
5
6
7
8
PS C:\> function Get-ServicePacks ($file) {
>> $names = get-content $file
>> foreach ($name in $names) {
>> $name
>> gwmi win32_operatingsystem -prop servicepackmajorversion -comp $name
>> }
>> }
>>


As you can see, not much has actually changed. This simply encloses the previous example in a function named Get-ServicePacks (in keeping with the Windows PowerShell verb-noun naming convention). The function now has an input parameter named $file, which has been substituted in the Get-Content cmdlet so that a different file can be specified when the function is run. Now that the function is defined, you can simply run it by calling its name, almost like a cmdlet, and passing the input parameter:
code:
1
PS C:\> Get-ServicePacks c:\computers.txt
Vervang het stukje om de file te parsen naar afzonderlijke namen naar een pure parameter en je bent er ook toch?
Of je zet de usernames die je wilt bewerken in een inputfile, zodat je er meerdere tegelijk kan doen.

ik heb een 864 GB floppydrive! - certified prutser - the social skills of a thermonuclear device

Pagina: 1