Link to home
Start Free TrialLog in
Avatar of Just_RC
Just_RCFlag for United States of America

asked on

PSCredential (function) in PS profile

Experts:
Good evening.  If I could get this to work it'd sure be helpful.  We have a platform exposed to a power-shell module to which we have to authenticate to.  I put the following function in my power-shell profile:

function Set-DefaultCreds {
$passwd = ConvertTo-SecureString "mypasswd" -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ("admin", $passwd)
}

Now...when I launch into a powershell session, I can type Set-Default  [tab] and it auto-completes, however I get no value from "$creds"

Any thoughts would be greatly appreciated.

Have a great week.
Avatar of Aard Vark
Aard Vark
Flag of Australia image

Well the scope of $creds is local to the function. If you want to make it available globally in the script, you will need to make it have global scope.

function Set-DefaultCreds
{
   $passwd = ConvertTo-SecureString "mypasswd" -AsPlainText -Force
   $global:creds = New-Object System.Management.Automation.PSCredential ("admin", $passwd)
}

Open in new window

Or

function Set-DefaultCreds
{
   $passwd = ConvertTo-SecureString "mypasswd" -AsPlainText -Force
   $creds = New-Object System.Management.Automation.PSCredential ("admin", $passwd)
   return $creds
}

Open in new window

Personally I would return the creds variable.

$creds = Set-DefaultCreds
Avatar of Just_RC

ASKER

Sorry..swamped at work.  No joy.  The object isn't there on-boot, but I can call it as before....

From my profile:

function Set-DefaultCreds
      {
      $passwd = ConvertTo-SecureString "admin12345!" -AsPlainText -Force
      $creds = New-Object System.Management.Automation.PSCredential ("admin", $passwd)
      }
Set-DefaultCreds

Then from my session:
PS D:\> $creds
PS D:\> Set-DefaultFreds
PS D:\> $creds
PS D:\>

..but I set them through the GUI....
PS D:\> $creds
PS D:\> get-credential

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential

UserName                                                                                                       Password
--------                                                                                                       --------
\admin                                                                                     System.Security.SecureString
ASKER CERTIFIED SOLUTION
Avatar of Aard Vark
Aard Vark
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Just_RC

ASKER

Thanks...
I will add that line, but I'm almost sure I tried that before.  

Will advise...and thank you again for your help.
Avatar of Just_RC

ASKER

I've requested that this question be closed as follows:

Accepted answer: 0 points for Just_RC's comment #a38389076

for the following reason:

return $creds within function did the trick.  Thank you.
I'm confused was the answer provided or not?