Script Help-IIS

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of cm cm 2 weeks, 5 days ago.

  • Author
    Posts
  • #57350
    Profile photo of cm
    cm
    Participant

    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.

    https://www.docdroid.net/nc8KWZI/unfinished-iisscript.txt.html

    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.

  • #57367
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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
    • #57613
      Profile photo of cm
      cm
      Participant

      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.

  • #57625
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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
    
  • #57632
    Profile photo of cm
    cm
    Participant

    This is what I've tried:

    https://www.docdroid.net/Inm3TDS/iis-script.txt.html

    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.