Showing posts with label execute cmdlets in 2003. Show all posts
Showing posts with label execute cmdlets in 2003. Show all posts

Wednesday, March 23, 2011

Pass credential with cmd as arugments to file and get its value for windows2003

Powershell with dynamic parameter addition. I suppose this would be the technique.
param([string]$user, [string]$hostname)
$cmd = "Get-WmiObject -class Win32_Service -computername `$hostname"
if($user -ne ""){
$myCred = Get-Credential $user
$cmd = $cmd + "-credential `$myCred"
}
Invoke-Expression $cmd


LIKE the one below can be taken as test.ps1 file and run to execute the cmdlets.


param($Argument1,$Name,$ParentContainer,$UserPassword,$email,$firstname,$Intials,$lastname,$samaccountname,$userprinicipalName,$displayName,$credntial);

$password = 'P@ssw0rd' | ConvertTo-SecureString -asPlainText -Force

$username = "msgdevexch3.local\administrator"

$credential = New-Object System.Management.Automation.PSCredential($username,$password);
$cmdExe = $Argument1+" -Name '"+$Name+"' -ParentContainer '"+$ParentContainer+"' -UserPassword '"+$UserPassword+"' -Email '"+$email+"'" + "-credential `$credential ";

$cmdExe

Add-PSSnapin Quest.ActiveRoles.ADManagement
invoke-Expression -command $cmdExe;