Hi when i try to do some code:
$Username = 'us'
$Password = 'password'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
powershell.exe -command "Invoke-Command -ComputerName server.com -scriptblock {pathCopyAndUnzip.ps1} -Credential $Cred"
This prompt me for a password but when i try to run this command like here (without powershell.exe):
Invoke-Command -ComputerName server.com -scriptblock {pathCopyAndUnzip.ps1} -Credential $Cred
it works without prompt. Do you know how to resolve that? I need to use option 1 because this command is runned from TFS build definition file like here:
<Exec Command="powershell.exe -command "Invoke-Command -ComputerName $(Server) -scriptblock {path} -Credential $Cred"" Condition="'$(RunTests)' == 'True'"/>
You could put your script into it's own file and then call that from TFS rather inline code.
C:\folder\script.ps1:
Param(
[string]$Username,
[string]$Password,
[string]$OtherParam,
)
$Password = $Password | ConvertTo-SecureString -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Password
Invoke-Command -ComputerName server.com -FilePath "C:\folder\CopyAndUnzip.ps1 -Something $OtherParam" -Credential $Cred
Then call it like so:
<Exec Command="powershell.exe -command "C:\folder\script.ps1 -username user10 -password P#55w0rd -OtherParam Whatever" Condition="'$(RunTests)' == 'True'"/>
You could try to pipe the commands to powershell.exe like this:
'$Username = "us"; $Password = "password"; $pass = ConvertTo-SecureString -AsPlainText $Password -Force; $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass; Invoke-Command -ComputerName server.com -scriptblock {pathCopyAndUnzip.ps1} -Credential $Cred' | powershell.exe -command -
<Exec Command="$(PsExecPath) -accepteula \\$(Server) cmd /C powershell -File FILEPATH " Condition="'$(RunTests)' == 'True'"/>
I used old good psExec :) Everything is work now.
Related
I have the following commands in a powershell script (within Jenkins):
$Command = "C:\Program Files\Microsoft SQL Server\150\DAC\bin\SqlPackage.exe"
$Parms = "/Action:Script /sf:DB.dacpac /Profile:publish.xml"
$Prms = $Parms.Split(" ")
& "$Command" $Prms
How can I run the SqlPackage.exe as another user?
PS: It is within Jenkins so I can't run the ps1 file or SqlPackage.exe using runas windows dialog.
EDIT:
I think I am very close, so far I have the following script.
$sb = [scriptblock]::create("& ""SqlPackage.exe"" ""/Action:Script /sf:DB.dacpac /Profile:publish.xml /TargetServerName:localdb /op:Publish.sql""")
$Secure_Password = ConvertTo-SecureString -String $parPassword -AsPlainText -Force
$Credential = New-Object -Type PSCredential($parUserId,$Secure_Password)
$Session = New-PSSession -ComputerName ServerName -Credential $Credential
Invoke-Command -Session $Session -ScriptBlock $sb
I am getting the following error:
*** Argument 'Action' has an invalid value: 'Script
/sf:DB.dacpac /Profile:publish.xml
/TargetServerName:localdb /op:Publish.sql'
instead of creating session, use start-process. I would recommend providing complete path to the dacpac file with SourceFile switch along with the Profile switch
$Command = "C:\Program Files\Microsoft SQL Server\150\DAC\bin\SqlPackage.exe"
$Parms = "/Action:Script /sf:DB.dacpac /Profile:publish.xml"
$Secure_Password = ConvertTo-SecureString -String $parPassword -AsPlainText -Force
$Credential = New-Object -Type PSCredential($parUserId,$Secure_Password)
Start-Process -Credentials $credential -FilePath $command -ArgumentList $Parms
I have a PowerShell Script that will invoke a command on a remote server. I'm trying to set this script up so that I can pass in a service parameter and it will drop that specific table in MongoDB
$service = "DatabaseName"
$username = "username"
$password = "password"
$pass = ConvertTo-SecureString -AsPlainText $password -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $pass
Invoke-Command -ComputerName Remote-Server1 -Credential $cred -ArgumentList $service -ScriptBlock {
param($service)
& 'C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe' $service --eval 'db.dropDatabase()'
}
Invoke-Command -ComputerName Remote-Server2 -Credential $cred -ArgumentList $service -ScriptBlock {
param($service)
& 'C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe' $service --eval 'db.dropDatabase()'
}
If I were to use the following it does not drop the MongoDB database:
& 'C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe' $service --eval 'db.dropDatabase()'
However, If I use the following it works as expected:
& 'C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe' DatabaseName --eval 'db.dropDatabase()'
Why does it work when I hard code that database name but not when I use a variable.
Since your code only needs $service in a read-only manner you can use the Using:-scope modifier, see additional information here.
Based on that you can change the code to:
$service = "DatabaseName"
$username = "username"
$password = "password"
$pass = ConvertTo-SecureString -AsPlainText $password -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $pass
Invoke-Command -ComputerName Remote-Server1 -Credential $cred -ArgumentList $service -ScriptBlock {
& 'C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe' $Using:service --eval 'db.dropDatabase()'
}
Invoke-Command -ComputerName Remote-Server2 -Credential $cred -ArgumentList $service -ScriptBlock {
& 'C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe' $Using:service --eval 'db.dropDatabase()'
}
When performing remote commands via Invoke-Command I either use the Using modifier of prefix the variables in the param block with remote. Based on that I can avoid "unwanted" behaviors (as the one you stated above).
So another alternative would be:
Invoke-Command -ComputerName Remote-Server1 -Credential $cred -ArgumentList $service -ScriptBlock {
param($remoteService)
& 'C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe' $remoteService --eval 'db.dropDatabase()'
}
I'm having issues passing parameters to Invoke-Command, I've tried using -Args and -ArgumentList to no avail.
function One {
$errcode = $args
$username = "Ron"
$password = ConvertTo-SecureString -String "Baxter" -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
$cred
$Result = Invoke-Command -ComputerName MyPc -ScriptBlock { & cmd.exe /c "C:\Scripts\test.bat" Param1 $errcode ; $lastexitcode} -Credential $cred
echo $result
}
One 10
You can update your function to pass in your parameter as $errcode rather than using $args, this is better code as it's less confusing. (I'd recommend readng up on parameters and functions as it'll certainly help)
Then you need to pass $errcode into Invoke-Command using the ArgumentList parameter, and use $args[0] in its place:
function One ($errcode) {
$username = "Ron"
$password = ConvertTo-SecureString -String "Baxter" -AsPlainText -Force
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
$Result = Invoke-Command -ComputerName MyPc -ScriptBlock { & cmd.exe /c "C:\Scripts\test.bat" Param1 $args[0] ; $lastexitcode} -Credential $cred -ArgumentList $errcode
echo $Result
}
One 10
remotePSExecuter.ps1 file:
$InputArgs=$args[0]
$Username=$args[1]
$Password=$args[2]
$ComputerName=$args[3]
$PSToExecute=$args[4]
write-host $Username $Password $ComputerName $PSToExecute
$SecurePassWord = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object -TypeName "System.Management.Automation.PSCredential" $Username, $SecurePassWord
$Session = New-PSSession -ComputerName $ComputerName -credential $Cred
$DownloadInstallJob = Invoke-Command -Session $Session -filepath "${PSToExecute}" -ArgumentList "${InputArgs}"
echo $DownloadInstallJob
Remove-PSSession -Session $Session
downloadInstallWinVcops.ps1 file:
$buildId=$args[0]
$storageDir = "C:\vcops-downloads"
$webclient = New-Object System.Net.WebClient
$url = "http://build-squid.com/build/mts/release/${buildId}/publish/Web_Installers/InstData/Windows/VM/file.exe"
write-host url=$url
write-host downloading windows vcops build ${buildId} ...
$file = "$storageDir\winvcops.exe"
$webclient.DownloadFile($url,$file)
write-host downloading windows vcops finished!
write-host installing windows vcops...
Start-Process -FilePath "C:\vcops-downloads\winvcops.exe" -ArgumentList "-i silent" -wait
write-host windows vcops installation finished!
This is the way I am calling on Jenkins machine:
.\EnterpriseAdapters/remotePSExecuter.ps1 sb-xxxx Administrator password 10.xx.xx.xx EnterpriseAdapters/downloadInstallWinVcops.ps1
Problem:
Start-Process -FilePath "C:\vcops-downloads\winvcops.exe" -ArgumentList "-i silent" -wait is not getting called on remote machine.
I have a simple snippet I can run no problems within the powershell console. When I compile it to an EXE, or even a ps1 and run it, it doesn't find the reg value, no idea why.
Here is the code:
$User = "Training\Administrator"
$PWord = ConvertTo-SecureString -String "P#ssWord" -AsPlainText -Force
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
$creds = $Credentials
enter-pssession –computername Win7Client –credential $creds
Start-Sleep -s 2
Set-itemproperty “HKLM:\SOFTWARE\Citrix\Metaframe Password Manager\Extensions\SyncManager\Syncs\DefaultSync\Servers” -name Server1 -value \\DFSI\CPMStore
Return
I would change the last lines to:
$Pssn = new-psssession –computername Win7Client –credential $creds
invoke-command -Session $Pssn -scriptblock {Set-itemproperty “HKLM:\SOFTWARE\Citrix\Metaframe Password Manager\Extensions\SyncManager\Syncs\DefaultSync\Servers” -name Server1 -value \\DFSI\CPMStore }
Return
Hope this helps,
Luc