Powershell Get Scripts in Batch script and output to log file

I need help with powershell batch script. I have these Backup IIS and 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.

#BACKUP IIS
$folderExists = Test-Path -Path $env:Windir\System32\inetsrv\backup\IISBackup

if($folderExists)
{
    $timestamp = Get-Date -Format "yyyyMMdd hhmmss"

    $newName = "IISBackup" + $timestamp

    Rename-Item -Path $env:Windir\System32\inetsrv\backup\IISBackup -NewName $newName
}

Backup-WebConfiguration -Name "IISBackup"

#Display Current Configuration
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'

Raj,
Welcome to the forum. :wave:t3:

When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to help you spending potentially a lot of effort in vain. :wink:

You simply pipe the output to …

Here y oucan read about how to use parameters

So, the Out-File command should be specified after each command? Something like this? Will it append?


#BACKUP IIS
$folderExists = Test-Path -Path $env:Windir\System32\inetsrv\backup\IISBackup

if($folderExists)
{
    $timestamp = Get-Date -Format "yyyyMMdd hhmmss"

    $newName = "IISBackup" + $timestamp

    Rename-Item -Path $env:Windir\System32\inetsrv\backup\IISBackup -NewName $newName
}

Backup-WebConfiguration -Name "IISBackup" | Out-File -FilePath C:\Temp\IISWebConfiguationFile.Log

#Display Current Configuration
Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/<website name>' -filter 'system.web/authentication/forms' -name 'requireSSL' | Out-File -FilePath C:\Temp\IISWebConfiguationFile.Log -Append
Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/<website name>' -filter 'system.web/authentication/forms' -name 'cookieless' | Out-File -FilePath C:\Temp\IISWebConfiguationFile.Log -Append
Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/<website name>' -filter 'system.web/authentication/forms' -name 'protection' | Out-File -FilePath C:\Temp\IISWebConfiguationFile.Log -Append
Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location '' -filter 'system.webServer/security/access' -name 'sslFlags' | Out-File -FilePath C:\Temp\IISWebConfiguationFile.Log -Append
Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/' -filter 'system.web/authentication/forms/credentials' -name 'passwordFormat' | Out-File -FilePath C:\Temp\IISWebConfiguationFile.Log -Append

Would it hurt if you tried it? :wink:

I guess my question was, is that the only option where we specify Out-File after each command?

Why? What’s wrong with it?

Nothing is wrong. I wanted to know whether there is any easier option. I guess not. Thanks!

It depends on what you consider “easier”. :smirk: … in the vast majority of the cases there are at least a few different options to achieve a goal.

I am getting error on this. Is it because the $websitename is between the single quotes? Any idea on this?

$websitename = $args[0]

Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/$websitename' -filter 'system.web/authentication/forms' -name 'requireSSL' | Out-File -FilePath C:\Temp\IISWebConfiguationFile.Log -Append

Yes. Could you please at least try to search for a solution first by yourself before you come here and ask for help? Really often it will take only seconds to find a solution because in the vast majority of the cases you’re not the very first one with a given task or with the same or a similar issue with your code.

Variables will not be expanded in single quotes.