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 for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.