User1493
User1493

Reputation: 491

Credential prompts shows up though I embedded in the script in PowerShell

I have this PowerShell script which runs perfectly when I open Windows PowerShell ISE and execute it. This script has username and password embedded in it. No prompts rise asking for password when I execute this.

$encrypted = Get-Content E:\servers\Disk Space Monitoring\password.txt |ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential("\svc_user", $encrypted)
New-PSDrive -Name TARGET -PSProvider FileSystem -Root "\\**.**.***.**\Dev\VI_ONE\Backups\TM1 10.2" -Credential $credential
New-PSDrive -Name SOURCE -PSProvider FileSystem -Root "**.**.***.**\servers" -Credential $credential

But when I execute the same script by using mouse right-click → Run with Powershell, two prompts pops up (for each PSDrive respectively) asking for user name and password.

The same happens when I call the PowerShell script via batch script.

How to avoid this prompt?

FYI, I have stored the password as an encrypted file. Please ignore to which directory I'm moving to as I have *ed them.

Upvotes: 1

Views: 471

Answers (1)

User1493
User1493

Reputation: 491

Password files can be copied from servers to servers. It is not mandatory that it should be encrypted in individual servers. The above condition is needed only if you encrypt the file without a key. If you use a key and encrypt the file, then it can be shared across and used.

$encrypted = Get-Content E:\tm1servers\Disk_Space_Monitoring\password.txt | ConvertTo-SecureString -Key (1..16)
$credential = New-Object System.Management.Automation.PsCredential("domain.com\svc-tm1q", $encrypted)

while encrypting the file use the same key -Key (1..16)

Upvotes: 0

Related Questions