Powershell Get Scripts in Batch script and output to log file

SQL 321 Reputation points
2023-04-10T21:06:09.1866667+00:00

I need help with powershell batch script. I have these 5 powershell Get Scripts that I want to run in powershell batch. I also want the output of all these redirected to Log File and also pass the <website name> as parameters.

Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/<website name>' -filter 'system.web/authentication/forms' -name 'requireSSL'
Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/<website name>' -filter 'system.web/authentication/forms' -name 'cookieless'
Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/<website name>' -filter 'system.web/authentication/forms' -name 'protection'
Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location '' -filter 'system.webServer/security/access' -name 'sslFlags'
Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/' -filter 'system.web/authentication/forms/credentials' -name 'passwordFormat'

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,383 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,089 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 30,121 Reputation points Microsoft Vendor
    2023-04-12T03:00:51.9766667+00:00

    Hi, You can add a Param block to your script and append Out-File to each Get-WebConfigurationProperty cmdlet, then you can run the script with the WebsiteName parameter.

    Param(
        [Parameter(Mandatory=$true)]
        $WebsiteName
    )
    
    $log = "C:\Temp\log.txt"
    
    Get-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST/$WebsiteName" -filter 'system.web/authentication/forms' -name 'requireSSL' | Out-File -FilePath $log -Append
    Get-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST/$WebsiteName" -filter 'system.web/authentication/forms' -name 'cookieless' | Out-File -FilePath $log -Append
    Get-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST/$WebsiteName" -filter 'system.web/authentication/forms' -name 'protection' | Out-File -FilePath $log -Append
    Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location '' -filter 'system.webServer/security/access' -name 'sslFlags' | Out-File -FilePath $log -Append
    Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/' -filter 'system.web/authentication/forms/credentials' -name 'passwordFormat' | Out-File -FilePath $log -Append
    

    Best Regards,

    Ian Xue

    If the Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments