Tagged: powershell iis script
November 10, 2016 at 3:14 pm #57350
Hey all, firstly, appreciate any help or advice given.
I'm trying to write a script that will archive off IIS logs from servers in an OU to a network share, with the name of the server plus a mmddyyyy format. First I want to back them up to a temp directory on the local server before moving them to the network share. Here's what I have so far, be as harsh as you can if I'm in the totally wrong direction. Code runs but does nothing.
To clarify a bit on the script, there are two OUs I'm attempting to search from then checks if IIS is installed on them, then proceeds from there.
November 10, 2016 at 6:51 pm #57367
You've got a couple problems
- Your are setting values to null, not really necessary as Powershell will clean everything up
- Your foreach ($objItem in $Servers) should be foreach ($server in $servers) as most of your references are for $server
- Your Get-ChildItem is referencing a local path (e.g. C:, D:), but it should be a UNC for remote ("\\" + $Server + "\c$\Windows\temp\*.log*")
- There is no need to do a for loop of the files, leverage the pipeline. Get-ChildItem ("\\" + $Server + "\c$\Windows\temp\*.log*") | Move-Item -Source $LogArchive
- Lastly, if you want to generate a path, it's a bit easier to force create a file that will create the entire directory. You also don't need to use Test-Path, because you are forcing the path to be created, however, you'll need to capture errors from the command: New-Item ($env:Temp + "\Directory\Does\Not\Exist\test.txt") -ItemType File -Force
November 14, 2016 at 2:47 pm #57613
Ok, that seems to make more sense. Before I post my updated script (to ensure it's right), I had one other question:
Would I completely just remove the (If) test-path and then on a new line underneath add New-Item ($env:Temp + "\Directory\Does\Not\Exist\test.txt") -ItemType File -Force ?
Also, the way you stated to just pipe out get-child doesn't seem like it has any way of telling it to move to the temp folder on the local server first (before moved to archive path), unless I'm being dumb and misinterpreting it.
November 14, 2016 at 4:17 pm #57625
If you are generating a path by forcing file creation, then there is no need to test if the path exists. However, you still need to wrap the New-Item command in a try\catch to ensure that it could create the patch and didn't get a Access Denied or some other exception.
So, my first question is WHY are you moving it to TEMP and then to a remote share? No, there isn't a way to tell it to stage it in TEMP first, you would do something like this:
Get-ChildItem C:\Inetpub\Mylogs\*.log | Copy-Item C:\Windows\Temp\IISStaging Get-ChildItem C:\Windows\Temp\IISStaging\*.log | Copy-Item \\server\share\IISLogs
November 14, 2016 at 6:01 pm #57632
This is what I've tried:
With errors being thrown up about RPC Server being unavailable and particularly this line of code:
$iis = get-wmiobject Win32_Service -ComputerName $server -Filter "name='W3SVC'"
Cleared it to just go from Server to Network Share.
You must be logged in to reply to this topic.