[# POWERSHELL] HowTo: Convert read-host restult to a String

image47dd1de4

Hi,

in these days of deployment, playing fun moments to learn new things. One of them, and that gave us a headache for several minutes with Javi, was in the creation of an Application Pool using PowerShell. The following code is copied 100% an IIS.NET, for the creation of an Application Pool in IIS 7.0.

   1: Import-Module WebAdministration

   2: $userName = "ElBruno\Valentino"

   3: $password = read-host "Type user password for Application Pool for >> $userName"

   4:  

   5: write-host "set app pool properties"

   6: $appPool.processModel.userName = $userName

   7: $appPool.processModel.password = $password

   8: $appPool.managedRuntimeVersion = "v4.0"

   9: $appPool | set-item

If you try 99 environments, will surely work correctly, however to my touched me to try it in that environment number 100 where we had a type error when assigning the Password. Researching a bit, I have seen that the class ReadHost doesn’t return a type string, but something a little "more complicated", by default the return value is assigned to aSecureStringtype. From there who at the time of assigning the Password is possible that we have wrong permissions or access.

But worry that the solution only consists of "castear" to a string value that returns ReadHost. THE following example, evidenced on line 3

   1: Import-Module WebAdministration

   2: $userName = "ElBruno\Valentino"

   3: [string] $password = read-host "Type user password for Application Pool for >> $userName"

   4:  

   5: write-host "set app pool properties"

   6: $appPool.processModel.userName = $userName

   7: $appPool.processModel.password = $password

   8: $appPool.managedRuntimeVersion = "v4.0"

   9: $appPool | set-item

Greetings @ La Finca

The Bruno

Reference: http://ss64.com/ps/read-host.html

Advertisements

2 thoughts on “[# POWERSHELL] HowTo: Convert read-host restult to a String”

  1. Pingback: URL
  2. My coder is trying to convince me to move to .net from PHP.
    I have always disliked the idea because of the costs.
    But he’s tryiong none the less. I’ve been using WordPress on various websites for
    about a year and am concerned about switching to another platform.
    I have heard very good things about blogengine.

    net. Is there a way I can transfer all my wordpress posts into it?
    Any kind of help would be really appreciated!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.