Powershell retrieving cert by Thumbprint as string versus string variable2019 Community Moderator ElectionSetting Windows PowerShell path variableHow do I concatenate strings and variables in PowerShell?Powershell remote install .exe file errors and not finding setup.iss fileHow to pass an xml element to a scriptBlock when using invoke-commandValidate certificate chain with powershellIssues with Powershell Invoke-CommandModifying SSL cert check Powershell script to loop through multiple sitesConnect remote server and execute scriptSearch for a file and assign a path as a variable as a stringPowershell script to scan for Expired SSL certificate for all server in OU not working

The (Easy) Road to Code

PTIJ: Sport in the Torah

Vector-transposing function

Professor forcing me to attend a conference, I can't afford even with 50% funding

Sort array by month and year

Giving a talk in my old university, how prominently should I tell students my salary?

Can I negotiate a patent idea for a raise, under French law?

What would be the most expensive material to an intergalactic society?

Create chunks from an array

Can multiple states demand income tax from an LLC?

Why does a car's steering wheel get lighter with increasing speed

Tabular environment - text vertically positions itself by bottom of tikz picture in adjacent cell

Short story about an infectious indestructible metal bar?

How does learning spells work when leveling a multiclass character?

Should I file my taxes? No income, unemployed, but paid 2k in student loan interest

Use Mercury as quenching liquid for swords?

Why do we call complex numbers “numbers” but we don’t consider 2-vectors numbers?

3.5% Interest Student Loan or use all of my savings on Tuition?

Has a sovereign Communist government ever run, and conceded loss, on a fair election?

What is the purpose of a disclaimer like "this is not legal advice"?

I am the light that shines in the dark

Unidentified signals on FT8 frequencies

How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?

Is the differential, dp, exact or not?



Powershell retrieving cert by Thumbprint as string versus string variable



2019 Community Moderator ElectionSetting Windows PowerShell path variableHow do I concatenate strings and variables in PowerShell?Powershell remote install .exe file errors and not finding setup.iss fileHow to pass an xml element to a scriptBlock when using invoke-commandValidate certificate chain with powershellIssues with Powershell Invoke-CommandModifying SSL cert check Powershell script to loop through multiple sitesConnect remote server and execute scriptSearch for a file and assign a path as a variable as a stringPowershell script to scan for Expired SSL certificate for all server in OU not working










2















I'm trying to piece together some PowerShell code to loop through a list of servers, return some info regarding their IIS sites and bindings, and if they have an https binding, get the certificateHash and use that locate the cert by thumbprint and return its expiration date.



The problem I am having is, when i run my code below $binding.cerficateHash seems to return what I would expect, a string of the cert Hash, but when I use that certificateHash property to try and get the cert by its thumbprint, it doesnt work... but when I take the raw string value of the certificateHash value and hardcode it, it works...



I've inspected the certificateHash.GetType() and it appears to be just a string, so i dont understand what im doing wrong, and ive tried a handful of things, with no avail, granted this is my first crack at powershell so there's lots I don't know.



$sites = Invoke-Command -ComputerName $serverName Import-Module WebAdministration; Get-ChildItem -path IIS:Sites -ErrorAction SilentlyContinue 

foreach($site in $sites)

$serverName
$site.name
$site.physicalPath

foreach($binding in $site.bindings.Collection)

$binding.protocol
$binding.bindingInformation
$binding.certificateHash
$binding.certificateStoreName

if($binding.certificateHash)

# This outputs AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
$binding.certificateHash

# this retrieves a cert and returns its expiration date, Woohooo!
Start-Job Invoke-Command -ComputerName $serverName -ScriptBlock (Get-ChildItem -path Cert:LocalMachineWebHosting

# this does not find a cert, and ive tried many things, and no dice.
Start-Job Invoke-Command -ComputerName $serverName -ScriptBlock (Get-ChildItem -path Cert:LocalMachineWebHosting

# i've tried extracting the hash via "tostring" and using that, no dice
$hash = $binding.certificateHash.ToString()
Start-Job Invoke-Command -ComputerName $serverName -ScriptBlock Where-Object $_.Thumbprint -eq $hash )[0].GetExpirationDateString()

# i've tried adding some wildcards and using the -like operator, no dice.
$hash = "*" + $binding.certificateHash + "*"
Start-Job Invoke-Command -ComputerName $serverName -ScriptBlock Where-Object $_.Thumbprint -lilke $hash )[0].GetExpirationDateString()





Example output for a site.



  • Site1

  • D:Appssite1

  • http

  • *:80:Site1-test.ourdomain.com

  • https

  • *:443:Site1-test.ourdomain.com

  • AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

  • WebHosting









share|improve this question
























  • oh interesting, so when you invoke the command you are invoking it on remote machine, so you are running in the "context" of that machine. so you're saying it doesnt know what that variable is... is there a way to pass that info without hardcoding it?

    – Dylan Hayes
    2 days ago
















2















I'm trying to piece together some PowerShell code to loop through a list of servers, return some info regarding their IIS sites and bindings, and if they have an https binding, get the certificateHash and use that locate the cert by thumbprint and return its expiration date.



The problem I am having is, when i run my code below $binding.cerficateHash seems to return what I would expect, a string of the cert Hash, but when I use that certificateHash property to try and get the cert by its thumbprint, it doesnt work... but when I take the raw string value of the certificateHash value and hardcode it, it works...



I've inspected the certificateHash.GetType() and it appears to be just a string, so i dont understand what im doing wrong, and ive tried a handful of things, with no avail, granted this is my first crack at powershell so there's lots I don't know.



$sites = Invoke-Command -ComputerName $serverName Import-Module WebAdministration; Get-ChildItem -path IIS:Sites -ErrorAction SilentlyContinue 

foreach($site in $sites)

$serverName
$site.name
$site.physicalPath

foreach($binding in $site.bindings.Collection)

$binding.protocol
$binding.bindingInformation
$binding.certificateHash
$binding.certificateStoreName

if($binding.certificateHash)

# This outputs AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
$binding.certificateHash

# this retrieves a cert and returns its expiration date, Woohooo!
Start-Job Invoke-Command -ComputerName $serverName -ScriptBlock (Get-ChildItem -path Cert:LocalMachineWebHosting

# this does not find a cert, and ive tried many things, and no dice.
Start-Job Invoke-Command -ComputerName $serverName -ScriptBlock (Get-ChildItem -path Cert:LocalMachineWebHosting

# i've tried extracting the hash via "tostring" and using that, no dice
$hash = $binding.certificateHash.ToString()
Start-Job Invoke-Command -ComputerName $serverName -ScriptBlock Where-Object $_.Thumbprint -eq $hash )[0].GetExpirationDateString()

# i've tried adding some wildcards and using the -like operator, no dice.
$hash = "*" + $binding.certificateHash + "*"
Start-Job Invoke-Command -ComputerName $serverName -ScriptBlock Where-Object $_.Thumbprint -lilke $hash )[0].GetExpirationDateString()





Example output for a site.



  • Site1

  • D:Appssite1

  • http

  • *:80:Site1-test.ourdomain.com

  • https

  • *:443:Site1-test.ourdomain.com

  • AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

  • WebHosting









share|improve this question
























  • oh interesting, so when you invoke the command you are invoking it on remote machine, so you are running in the "context" of that machine. so you're saying it doesnt know what that variable is... is there a way to pass that info without hardcoding it?

    – Dylan Hayes
    2 days ago














2












2








2








I'm trying to piece together some PowerShell code to loop through a list of servers, return some info regarding their IIS sites and bindings, and if they have an https binding, get the certificateHash and use that locate the cert by thumbprint and return its expiration date.



The problem I am having is, when i run my code below $binding.cerficateHash seems to return what I would expect, a string of the cert Hash, but when I use that certificateHash property to try and get the cert by its thumbprint, it doesnt work... but when I take the raw string value of the certificateHash value and hardcode it, it works...



I've inspected the certificateHash.GetType() and it appears to be just a string, so i dont understand what im doing wrong, and ive tried a handful of things, with no avail, granted this is my first crack at powershell so there's lots I don't know.



$sites = Invoke-Command -ComputerName $serverName Import-Module WebAdministration; Get-ChildItem -path IIS:Sites -ErrorAction SilentlyContinue 

foreach($site in $sites)

$serverName
$site.name
$site.physicalPath

foreach($binding in $site.bindings.Collection)

$binding.protocol
$binding.bindingInformation
$binding.certificateHash
$binding.certificateStoreName

if($binding.certificateHash)

# This outputs AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
$binding.certificateHash

# this retrieves a cert and returns its expiration date, Woohooo!
Start-Job Invoke-Command -ComputerName $serverName -ScriptBlock (Get-ChildItem -path Cert:LocalMachineWebHosting

# this does not find a cert, and ive tried many things, and no dice.
Start-Job Invoke-Command -ComputerName $serverName -ScriptBlock (Get-ChildItem -path Cert:LocalMachineWebHosting

# i've tried extracting the hash via "tostring" and using that, no dice
$hash = $binding.certificateHash.ToString()
Start-Job Invoke-Command -ComputerName $serverName -ScriptBlock Where-Object $_.Thumbprint -eq $hash )[0].GetExpirationDateString()

# i've tried adding some wildcards and using the -like operator, no dice.
$hash = "*" + $binding.certificateHash + "*"
Start-Job Invoke-Command -ComputerName $serverName -ScriptBlock Where-Object $_.Thumbprint -lilke $hash )[0].GetExpirationDateString()





Example output for a site.



  • Site1

  • D:Appssite1

  • http

  • *:80:Site1-test.ourdomain.com

  • https

  • *:443:Site1-test.ourdomain.com

  • AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

  • WebHosting









share|improve this question
















I'm trying to piece together some PowerShell code to loop through a list of servers, return some info regarding their IIS sites and bindings, and if they have an https binding, get the certificateHash and use that locate the cert by thumbprint and return its expiration date.



The problem I am having is, when i run my code below $binding.cerficateHash seems to return what I would expect, a string of the cert Hash, but when I use that certificateHash property to try and get the cert by its thumbprint, it doesnt work... but when I take the raw string value of the certificateHash value and hardcode it, it works...



I've inspected the certificateHash.GetType() and it appears to be just a string, so i dont understand what im doing wrong, and ive tried a handful of things, with no avail, granted this is my first crack at powershell so there's lots I don't know.



$sites = Invoke-Command -ComputerName $serverName Import-Module WebAdministration; Get-ChildItem -path IIS:Sites -ErrorAction SilentlyContinue 

foreach($site in $sites)

$serverName
$site.name
$site.physicalPath

foreach($binding in $site.bindings.Collection)

$binding.protocol
$binding.bindingInformation
$binding.certificateHash
$binding.certificateStoreName

if($binding.certificateHash)

# This outputs AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
$binding.certificateHash

# this retrieves a cert and returns its expiration date, Woohooo!
Start-Job Invoke-Command -ComputerName $serverName -ScriptBlock (Get-ChildItem -path Cert:LocalMachineWebHosting

# this does not find a cert, and ive tried many things, and no dice.
Start-Job Invoke-Command -ComputerName $serverName -ScriptBlock (Get-ChildItem -path Cert:LocalMachineWebHosting

# i've tried extracting the hash via "tostring" and using that, no dice
$hash = $binding.certificateHash.ToString()
Start-Job Invoke-Command -ComputerName $serverName -ScriptBlock Where-Object $_.Thumbprint -eq $hash )[0].GetExpirationDateString()

# i've tried adding some wildcards and using the -like operator, no dice.
$hash = "*" + $binding.certificateHash + "*"
Start-Job Invoke-Command -ComputerName $serverName -ScriptBlock Where-Object $_.Thumbprint -lilke $hash )[0].GetExpirationDateString()





Example output for a site.



  • Site1

  • D:Appssite1

  • http

  • *:80:Site1-test.ourdomain.com

  • https

  • *:443:Site1-test.ourdomain.com

  • AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

  • WebHosting






powershell iis ssl-certificate invoke-command






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago







Dylan Hayes

















asked 2 days ago









Dylan HayesDylan Hayes

1,88111830




1,88111830












  • oh interesting, so when you invoke the command you are invoking it on remote machine, so you are running in the "context" of that machine. so you're saying it doesnt know what that variable is... is there a way to pass that info without hardcoding it?

    – Dylan Hayes
    2 days ago


















  • oh interesting, so when you invoke the command you are invoking it on remote machine, so you are running in the "context" of that machine. so you're saying it doesnt know what that variable is... is there a way to pass that info without hardcoding it?

    – Dylan Hayes
    2 days ago

















oh interesting, so when you invoke the command you are invoking it on remote machine, so you are running in the "context" of that machine. so you're saying it doesnt know what that variable is... is there a way to pass that info without hardcoding it?

– Dylan Hayes
2 days ago






oh interesting, so when you invoke the command you are invoking it on remote machine, so you are running in the "context" of that machine. so you're saying it doesnt know what that variable is... is there a way to pass that info without hardcoding it?

– Dylan Hayes
2 days ago













1 Answer
1






active

oldest

votes


















2














The computer you invoke the script block on doesn't know about the $binding variable in your local session. (That's also why it works when passing a literal string.)



Try passing the value as argument:



Invoke-Command -Computer $serverName -Script 
param ($hash)
(gci Cert:LocalMachineWebHosting -Arg $binding.certificateHash





share|improve this answer




















  • 1





    thank you big time! not only does that solve my issue, but i think you just helped me connect a pretty big dot when invoking commands on remote machines!

    – Dylan Hayes
    2 days ago











  • @DylanHayes Glad it worked. I never use remoting xD Good luck!

    – marsze
    2 days ago










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55027044%2fpowershell-retrieving-cert-by-thumbprint-as-string-versus-string-variable%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














The computer you invoke the script block on doesn't know about the $binding variable in your local session. (That's also why it works when passing a literal string.)



Try passing the value as argument:



Invoke-Command -Computer $serverName -Script 
param ($hash)
(gci Cert:LocalMachineWebHosting -Arg $binding.certificateHash





share|improve this answer




















  • 1





    thank you big time! not only does that solve my issue, but i think you just helped me connect a pretty big dot when invoking commands on remote machines!

    – Dylan Hayes
    2 days ago











  • @DylanHayes Glad it worked. I never use remoting xD Good luck!

    – marsze
    2 days ago















2














The computer you invoke the script block on doesn't know about the $binding variable in your local session. (That's also why it works when passing a literal string.)



Try passing the value as argument:



Invoke-Command -Computer $serverName -Script 
param ($hash)
(gci Cert:LocalMachineWebHosting -Arg $binding.certificateHash





share|improve this answer




















  • 1





    thank you big time! not only does that solve my issue, but i think you just helped me connect a pretty big dot when invoking commands on remote machines!

    – Dylan Hayes
    2 days ago











  • @DylanHayes Glad it worked. I never use remoting xD Good luck!

    – marsze
    2 days ago













2












2








2







The computer you invoke the script block on doesn't know about the $binding variable in your local session. (That's also why it works when passing a literal string.)



Try passing the value as argument:



Invoke-Command -Computer $serverName -Script 
param ($hash)
(gci Cert:LocalMachineWebHosting -Arg $binding.certificateHash





share|improve this answer















The computer you invoke the script block on doesn't know about the $binding variable in your local session. (That's also why it works when passing a literal string.)



Try passing the value as argument:



Invoke-Command -Computer $serverName -Script 
param ($hash)
(gci Cert:LocalMachineWebHosting -Arg $binding.certificateHash






share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered 2 days ago









marszemarsze

5,52732041




5,52732041







  • 1





    thank you big time! not only does that solve my issue, but i think you just helped me connect a pretty big dot when invoking commands on remote machines!

    – Dylan Hayes
    2 days ago











  • @DylanHayes Glad it worked. I never use remoting xD Good luck!

    – marsze
    2 days ago












  • 1





    thank you big time! not only does that solve my issue, but i think you just helped me connect a pretty big dot when invoking commands on remote machines!

    – Dylan Hayes
    2 days ago











  • @DylanHayes Glad it worked. I never use remoting xD Good luck!

    – marsze
    2 days ago







1




1





thank you big time! not only does that solve my issue, but i think you just helped me connect a pretty big dot when invoking commands on remote machines!

– Dylan Hayes
2 days ago





thank you big time! not only does that solve my issue, but i think you just helped me connect a pretty big dot when invoking commands on remote machines!

– Dylan Hayes
2 days ago













@DylanHayes Glad it worked. I never use remoting xD Good luck!

– marsze
2 days ago





@DylanHayes Glad it worked. I never use remoting xD Good luck!

– marsze
2 days ago



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55027044%2fpowershell-retrieving-cert-by-thumbprint-as-string-versus-string-variable%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page