none
script issue

    Question

  • I am getting a token error when running this script.  any help on this and hwo to correct..

    cd C:\Scripts

    $Days = “15?
    $Now = Get-Date
    $LastWrite = $Now.AddDays(-$days)
    $server = get-content servers.txt
    foreach ($node in $server)
    {
    get-childitem -recurse “\\$node\C$\test\*log ” | Where-Object {$_.LastWriteTime -le $LastWrite} | move-item c:\Logfiles\Archive


    }

    Wednesday, April 04, 2012 7:04 PM

Answers

All replies

  • Wednesday, April 04, 2012 7:10 PM
  • this is error I get below..

    PS C:\Scripts> .\IISCleanup.ps1
    Unexpected token '\\$node\C$\test\*log' in expression or statement.
    At C:\Scripts\IISCleanup.ps1:9 char:45
    + get-childitem -recurse "\\$node\C$\test\*log <<<<  " | Where-Object {$_.LastWriteTime -
    Logfiles\Archive
        + CategoryInfo          : ParserError: (\\$node\C$\test\*log:String) [], ParseExcepti
        + FullyQualifiedErrorId : UnexpectedToken

    Wednesday, April 04, 2012 7:13 PM
  • Hi,

    The -recurse parameter is a switch that does not use an argument.

    You can write it as:

    get-childitem "\\$node\C$\test\*log" -recurse

    or

    get-childitem -recurse -path "\\$node\C$\test\*log"

    HTH,

    Bill

    • Proposed as answer by Bigteddy Wednesday, April 04, 2012 7:35 PM
    Wednesday, April 04, 2012 7:24 PM
  • Thanks.  I used the below and got error below now. 

    get-childitem "\\$node\C$\test\*log" -recurse

    The string starting:
    PS C:\Scripts> .\IISCleanup.ps1
    Unexpected token '\\$node\C$\test\*log' in expression or statement.
    At C:\Scripts\IISCleanup.ps1:9 char:36
    + get-childitem "\\$node\C$\test\*log <<<<  " -recurse | Where-Object {$_.LastWriteTime -
    Logfiles\Archive
        + CategoryInfo          : ParserError: (\\$node\C$\test\*log:String) [], ParseExcepti
        + FullyQualifiedErrorId : UnexpectedToken

    Wednesday, April 04, 2012 7:39 PM
  • This is script and error that is displaying after change suggested above.

    cd C:\Scripts

    $Days = “15?
    $Now = Get-Date
    $LastWrite = $Now.AddDays(-$days)
    $server = get-content servers.txt
    foreach ($node in $server)
    {
    get-childitem -recurse -path "\\$node\C$\test\*log" | Where-Object {$_.LastWriteTime -le $LastWrite} | move-item c:\Logfiles\Archive


    }

    Error below

    PS C:\Scripts> .\IISCleanup.ps1
    The string starting:
    At C:\Scripts\IISCleanup.ps1:9 char:51
    + get-childitem -recurse -path "\\$node\C$\test\*log <<<< " | Where-Object {$_.LastWriteTime -le $LastWrite} | move-ite
    m c:\Logfiles\Archive
    is missing the terminator: ".
    At C:\Scripts\IISCleanup.ps1:13 char:1
    +  <<<<
        + CategoryInfo          : ParserError: ( | Where-Object...chive
    }
    :String) [], ParseException
        + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

    Wednesday, April 04, 2012 8:34 PM
  • Inspect $node for quotes.  something is wrong with that.

    servers.txt should beone linepers server with only the nbname and \\ or $ in the name.  Servers need to be one per line. The file nees to be ANSI.


    ¯\_(ツ)_/¯

    Wednesday, April 04, 2012 8:55 PM
  • Thanks.  I tried to reserach this but not able to find where to correct.  I am new to this so not easy to find what to change..  Any help is appreciated.  thx..

    Thursday, April 05, 2012 2:55 PM
  • Shouldn't it be:

    $Days = "15"


    Richard Mueller - MVP Directory Services

    • Proposed as answer by jrv Thursday, April 05, 2012 5:22 PM
    • Marked as answer by Bill_StewartModerator Thursday, April 12, 2012 6:45 PM
    Thursday, April 05, 2012 3:13 PM
  • Shouldn't it be:

    $Days = "15"


    Richard Mueller - MVP Directory Services

    The web page does that when you paste items with quotes sometimes.  I get issue with links inside of quotes that totally break the edit box.  It doesn't happen when typing but will if you copy  link from a web page and paste it intothe edit box.  The box accepts HTML from the clipboard and seems to want to reformat it.

    The error seems to indicate that the string is the culprit:

     get-childitem -recurse -path "\\$node\C$\test\*log <<<< " | Where

    Note the <<<< is pointing to the string as if it is unterminated or has an iillegal character.

    This can happen if you read in a unicode file in a different character set than PowerSHell is using or if ther eare nulls in the file which can happen if you copy from a web page and paste into notepad.

    Some script editors also default to unicode for text files.


    ¯\_(ツ)_/¯

    Thursday, April 05, 2012 3:40 PM
  • thanks.  I changed to
    $Days = "15"
    and its not throwing error.  Just not executing moving anything as script should..
    Thursday, April 05, 2012 5:11 PM
  • thanks.  I changed to
    $Days = "15"
    and its not throwing error.  Just not executing moving anything as script should..

    I am duly amazed but after looking a bit furhter I can see how that would happen. 

    I asumed incorrectly that the ? was a pasting mistake so I fixed it in my test. 

    Reminder to self - never assume.

    Don't forget to mark Ricards answer.


    ¯\_(ツ)_/¯

    Thursday, April 05, 2012 5:24 PM