[# 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

[#POWERSHELL] HowTo: Transformar el resultado de read-host a un String

image47dd1de4

Buenas,

en estos días de despliegue, tocan momentos divertidos para aprender  nuevas cosillas. Una de ellas, y que nos dio un dolor de cabeza de varios minutos con Javi, fue en la creación de un Application Pool utilizando PowerShell. El siguiente código está copiado 100% de un IIS.Net, para la creación de un Application Pool en un IIS 7.0.

 
   1: Import-Module WebAdministration

   2:  

   3: $userName = "ElBruno\Valentino"

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

   5:  

   6: write-host "set app pool properties"

   7: $appPool.processModel.userName = $userName

   8: $appPool.processModel.password = $password

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

  10: $appPool | set-item

 

Si lo pruebas en 99 entornos, seguramente funcionará correctamente, sin embargo a mi me ha tocado probarlo en ese entorno número 100 donde teníamos un error de tipo al momento de asignar la Password. Investigando un poco, he visto que la clase ReadHost no retorna un tipo string, sino algo un poco “más complicado”, por defecto el valor retornado se asigna a un tipo SecureString. De allí que al momento de asignar la Password es posible que tengamos un error de permisos o de acceso.

Pero tranquilos que la solución solo consiste en “castear” a un string el valor que retorna ReadHost. EL siguiente ejemplo, lo demuestra en la línea 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

 

Saludos @ La Finca

El Bruno

   

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