So let's go back into our environment, and we'll look at how we create
PSCredential objects to be able to utilize that for other commands.
So we're back on our Windows 10 workstation, the one that's
connected to our Globomantics servers.
And one of the things that we want to do here is
obviously create a PSCredential object.
Now first off, I'm just going to create a variable here called $creds.
And the simplest and easiest way of creating a PSCredential
object is to call Get‑Credential, and I'm going to press Enter
here, and you'll see it launches a Windows prompt that's going to
ask me to enter credentials.
So if I type Test and type Test again, I'll click OK.
And sure enough, it creates it.
Now, I don't have an account called Test with a password of Test, but
what it did is it just creates a credential object.
So it would just be a way to not actually validating
the credentials that you type.
So if you type the password incorrectly, then, of course, it's going to fail.
So, what about if you wanted to actually create it with
valid credentials and then utilize that?
Well, if we just clear here, what we can actually do is
remake our variable called $creds.
Well, I'll tell you what we'll do first.
We'll actually just use whoami first, which will tell me my training account.
And sure enough, I'm using training domain and trainer.
I want to utilize that, so we'll now say $creds, and we'll say Get‑Credential.
And what we can do is initiate the first part of the credential here. So I can
put in the name, so I can say "training\trainer".
Now what will happen when I do this is it's already
prefixed the username for me.
So when I press Enter here, you'll see that my box for username
is already populated with training/trainer.
And then I can just type in my password like so, press Enter,
and then if I check what's in the $creds object, you can see
it's now got a training\trainer.
You'll never see the password because the password is a secure string, so a
very simple way of obviously utilizing the Get‑Credential. That is the most
common way of creating a PSCredential object.
Now another way is to do it, I have to refer to this as the more complicated
approach, but I'm a great fan of kind of granularity of things.
So I'm going to say $pwd. You can see I'm just creating simple
accounts, so I've got my training\trainer and then my password,
which isn't the correct one, by the way.
But what I'm able to do here is I can then start to create a new object.
Now the first thing you'll notice is that when I actually created the
$creds object previously, so if I just say $creds here,
you'll see actually it created what was called a System.Security.SecureString,
which clearly my password variable is not.
So, what we need to do is actually repopulate this
variable with a different approach.
So I'm going to say ConvertTo‑SecureString because that's what we want to do.
We want to take the piece of plain text that we've written
and convert it to a secure string value.
So I'm going to say "Pass@word1", and then I'm going to say
‑AsPlainText, and then I'll just ‑Force that to create it.
Okay,
so what does that look like in my password object? Well, now it
just looks like System.Security.SecureString,
which is exactly what we need as part of the PSCredential object.
So what we can do now, let me just clear this down so we've got a clear screen,
I'm going to create my $creds variable.
And the first way of doing this is to actually say New‑Object.
And then just like you would in any kind of coding language,
you can kind of traverse through the namespaces.
So, you'll see I'm going to say, System.Management.Automation,
and then I'm going to say if I tab through,
you'll see PS with a C. Now there's loads of them, so if I just say
PSCre, PSCredential. And then what the PSCredential object requires
is two objects to be present here.
The first one, as you've guessed it, would be the user,
the second one would be the password.
So if I press Enter now, and when I go to my $creds object,
you'll see that I've now got that same credential object.
So instead of me saying Get‑Credential and having
someone type the credentials at runtime,
I could've stored these within text objects inside a PowerShell script.
Now the other option I've got is to kind of, if we go back
to this same kind of syntax that's here,
what we can do is change it a little bit.
Now, of course, with PowerShell, we have the ability to utilize
various properties, so, for example ‑TypeName. This would, I
suppose, be the strongly‑typed version of this.
So if we go back, you can say my ‑TypeName is of PSCredential. And then
what I can do is pass an ‑ArgumentList, and the ‑ArgumentList is going
to be the variables that we populated.
So I'm going to press Enter here, and then sure
enough, you'll see my $creds object.
So, three different ways of actually creating the PSCredential object.
The first one being using Get‑Credential, and then the second one
being, obviously, creating the user and the password, and then
creating a new object and passing in the type or actually declaring
the ‑TypeName and the ‑ArgumentList.
All of these will work, and this will allow you to reuse that $creds
object whenever you need to connect to anything.
So if I need to connect to a different computer, so I can say
Get‑ComputerInfo, for example, and if I tab through the properties,
you'll see there's all the standard ones that are there,
you know, kind of I want to output the values,
I want a pipeline property,
whatever, on certain properties, you'll find that
you're able to pass a credential object.
Now this is more kind of useful when you're connecting to the cloud,
for example. You'll be able to say Get‑Credential, and it will pass
that object to whatever the command is in, for example, Azure or Office 365.