Sunday, February 17, 2019

Read a .INI File Value from Power shell...


How we can read value from .INI configuration file and assign into the power shell variable ? This task is quite interesting because reading and particular value from a file and use into the power shell can solve many manual effort some time. I have created one function or and you can call it subroutine which reads a value from the file and assign to respective variable. May be this will help you same where .





############################Read Value From .INI file ###############################


Function ReadIniFileValues                                                   
 {
      #$sIniFilePath = Split-Path $sScriptFolder -Parent
      $sIniFilePath = "C:\Temp\Example.ini"          # Mension .ini the file path here
      $sReadIniFileData = [IO.File]::ReadAllText("$sIniFilePath")

      $sConvertIniFileData =  ConvertFrom-StringData -StringData $sReadIniFileData

      # Reading Apptitle infomation from Example.ini file directly

      $Script:sAppTitle =$sConvertIniFileData.APPNAME
      $Script:sAppVer = $sConvertIniFileData.APPVERSION
      $Script:sUnInstallAppName = $sConvertIniFileData.UNINSTALLAPPNAME
      $Script:sUnInstallAppType = $sConvertIniFileData.UNINSTALLAPPTYPE
 }


ReadIniFileValues ""


#################################################################################



Note:- Please test script in your test environment before running in production directly.

No comments:

Post a Comment