Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 599
  • Last Modified:

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.
0
Just_RC
Asked:
Just_RC
  • 3
  • 3
1 Solution
 
LearnctxEngineerCommented:
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
0
 
Just_RCAuthor Commented:
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
0
 
LearnctxEngineerCommented:
If I understand you correctly. You're doing this in your PowerShell profile ($PROFILE). You would like $creds to be loaded every time you start PowerShell. Then the same problem will still apply. In your function, $creds scope is local to that function. That means it only exists inside that function unless you make the variable global or return the variable and store it in another global variable.

So, edit your profile so that it is this instead of what you're using.

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

Open in new window

$creds will then be available because you are returning the value from the local $creds variable to a global variable.
0
Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

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

Will advise...and thank you again for your help.
0
 
Just_RCAuthor Commented:
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.
0
 
LearnctxEngineerCommented:
I'm confused was the answer provided or not?
0

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now