none
Powershell errors

    問題

  • I get the following errors when trying to run this script - I'm a newbie so not sure what id happening can you explain - Please.

    

    ## Author: Adam Bacon ## Created on: 21/03/2011 ## Scripted in: Powershell will work in V1&2 ## Purpose: To give you the folder size of each share you have on your machine ##Modified: 13/06/2011 to include -append parameter $ErrorActionPreference = "silentlycontinue" $DebugPreference = "Continue" Write-Debug "Gathering share information" #Getting all shares on current computer Get-WmiObject -Class Win32_Share | select path | format-table -autosize | Out-File C:\shares.txt #Formatting the output replacing property headings and hyphens (Get-Content C:\shares.txt) | Foreach-Object {(($_.Replace("path", "")).Replace("----","").Replace(" ",""))}| Out-File C:\shares.txt #Removing any blank white spaces at the end of each line of text output Get-Content c:\shares.txt | foreach { $_.trimend()} | Out-File C:\shares.txt -Append -NoClobber #Removing the default shares so time does not get waisted searching these shares Get-content c:\shares.txt | where{$_.length -gt 3} | Out-File C:\shares.txt -Append -NoClobber #Now removing all non searchable path shares such as Admin Mono Laser IPC etc Get-Content c:\shares.txt | where { $_.chars(1) -contains ":"} | Out-File C:\shares.txt -Append -NoClobber #Again removing any trailing spaces so when the foldersizecsv script runs then all file paths will be searchable Get-Content c:\shares.txt | foreach { $_.trimend()} | Out-File C:\finish.txt Write-Debug "About to commence searching through all shares, please wait" $FinalShare = Get-Content C:\finish.txt | where {$_.trim()} foreach ($share in $FinalShare) { $objFldr = New-Object -com Scripting.FileSystemObject "Share Path = " + $objFldr.GetAbsolutePathName("$share") + "`n" + "Share Name = " + $objFldr.GetFileName("$share") + "`n" + "Size" +" = " + "{0:N4}" -f (($objFldr.GetFolder("$share").Size) / 1GB) + " GB" + "`n" } ======== PS C:\Users\dpuser> ## Author: Adam Bacon #

    Errors below:

    # Created

    on: 21/03/2011 ## Scripted in: Powershell will work in V1&2 ## Purpose: To give you the folder size of each share you have on your machine ##Modified: 13/06/2011 to include -append parameter $ErrorActionPreference = "silentlycontinue" $DebugPreference = "Continue" Write-Debug "Gathering share information" #Getting all shares on current computer Get-WmiObject -Class Win32_Share | select path | format-table -autosize | Out-File C:\shares.txt #Formatting the output replacing property headings and hyphens (Get-Content C:\shares.txt) | Foreach-Object {(($_.Replace("path", "")).Replace("----","").Replace(" ",""))}| Out-File C:\shares.txt #Removing any blank white spaces at the end of each line of text output Get-Content c:\shares.txt | foreach { $_.trimend()} | Out-File C:\shares.txt -Append -NoClobber #Removing the default shares so time does not get waisted searching these shares Get-content c:\shares.txt | where{$_.length -gt 3} | Out-File C:\shares.txt -Append -NoClobber #Now removing all non searchable path shares such as Admin Mono Laser IPC etc Get-Content c:\shares.txt | where { $_.chars(1) -contains ":"} | Out-File C:\shares.txt -Append -NoClobber #Again removing any trailing spaces so when the foldersizecsv script runs then all file paths will be searchable Get-Content c:\shares.txt | foreach { $_.trimend()} | Out-File C:\finish.txt Write-Debug "About to commence searching through all shares, please wait" $FinalShare = Get-Content C:\finish.txt | where {$_.trim()} foreach (($share) in $FinalShare) { $objFldr = New-Object -com Scripting.FileSystemObject "Share Path = " + $objFldr.GetAbsolutePathName("$share") + "`n" + "Share Name = " + $objFldr.GetFileName("$share") + "`n" + "Size" +" = " + "{0:N4}" -f (($objFldr.GetFolder("$share").Size) / 1GB) + " GB" + "`n" } Unexpected token 'in $FinalShare' in expression or statement. At line:24 char:19


    

    2012年3月2日 上午 05:01

解答

  • Here is a function that you can use to report on the share size for all of your computers.

    function Get-ShareSize {
        Param(
        [String[]]$ComputerName = $env:computername
        )

    Begin{$objFldr = New-Object -com Scripting.FileSystemObject}

    Process{
        foreach($Computer in $ComputerName){
            Get-WmiObject Win32_Share -ComputerName $Computer -Filter "not name like '%$'" | %{
                $Path = $_.Path -replace 'C:',"\\$Computer\c$"
                $Size = ($objFldr.GetFolder($Path).Size) / 1GB
                New-Object PSObject -Property @{
                Name = $_.Name
                Path = $Path
                Description = $_.Description
                Size = $Size
                }
            }
        }
    }
    } Get-ShareSize -ComputerName (get-content C:\serverlist.txt)


    Rich Prescott | Infrastructure Architect, Windows Engineer and PowerShell blogger | MCITP, MCTS, MCP

    Engineering Efficiency
    @Rich_Prescott
    Windows System Administration tool
    AD User Creation tool


    2012年3月2日 下午 10:51
    版主
  • Rich - if you test very carefull I think you will find that that only works against teh local computer because $_.Path is a local path like c:\test.

    Ther eis really no good way to get share sizes except to access the admin share.  YOu can use the local path as a hint as to how to build the admin share reference.

    if path i sc:\test and computer is serv1 then

    \\server1\c$\test is the folder admin  path.

    PS>$objFldr = New-Object -com Scripting.FileSystemObje
    PS>$objFldr.GetFolder('\\ws101\c$\scripts')
    Path             : \\ws101\c$\scripts
    Name             : scripts
    ShortPath        : \\ws101\c$\scripts
    ShortName        : scripts
    Drive            : System.__ComObject
    ParentFolder     : System.__ComObject
    Attributes       : 16
    DateCreated      : 3/30/2011 1:55:24 PM
    DateLastModified : 2/15/2012 11:33:49 AM
    DateLastAccessed : 2/15/2012 11:33:49 AM
    Type             : File Folder
    IsRootFolder     : False
    Size             : 3872358
    SubFolders       : System.__ComObject
    Files            : System.__ComObject


    ¯\_(ツ)_/¯

    Updated the code to reflect this.  My home lab is undergoing Windows 8 renovations, so I'm relegated to only using my local machine for the moment.

    Rich Prescott | Infrastructure Architect, Windows Engineer and PowerShell blogger | MCITP, MCTS, MCP

    Engineering Efficiency
    @Rich_Prescott
    Windows System Administration tool
    AD User Creation tool

    2012年3月2日 下午 11:36
    版主

所有回覆

  • Perhaps it would be easier if you explained what it is you are tryng to do.  Thge code does not make much sense except as a list of commands.

    It just looks like the same list of lines printed twice.


    ¯\_(ツ)_/¯


    • 已編輯 jrv 2012年3月2日 上午 05:34
    2012年3月2日 上午 05:33
  • The following does everything teh first 6 or 8 lines of your script are trying to do.

    Get-WmiObject -Class Win32_Share -filter "not name like '%$'" |  ft -auto -hide


    ¯\_(ツ)_/¯

    2012年3月2日 上午 05:42
  • You can replace this:

    Get-WmiObject -Class Win32_Share | select path | format-table -autosize | Out-File C:\shares.txt   
    #Formatting the output replacing property headings and hyphens   
    (Get-Content C:\shares.txt) | Foreach-Object {(($_.Replace("path", "")).Replace("----","").Replace("  ",""))}| Out-File C:\shares.txt   
    #Removing any blank white spaces at the end of each line of text output   
    Get-Content c:\shares.txt | foreach { $_.trimend()} | Out-File C:\shares.txt -Append -NoClobber  
    #Removing the default shares so time does not get waisted searching these shares   
    Get-content c:\shares.txt | where{$_.length -gt 3} | Out-File C:\shares.txt -Append -NoClobber   
    #Now removing all non searchable path shares such as Admin Mono Laser IPC etc   
    Get-Content c:\shares.txt | where { $_.chars(1) -contains ":"} | Out-File C:\shares.txt -Append -NoClobber   
    #Again removing any trailing spaces so when the foldersizecsv script runs then all file paths will be searchable   
    Get-Content c:\shares.txt | foreach { $_.trimend()} | Out-File C:\finish.txt   
    Write-Debug "About to commence searching through all shares, please wait"   

    By this:

    Get-WmiObject -Class Win32_Share | select path | foreach {$_.path | where {$_.length -gt 3 -and $_ -match ":"} } | out-file finish.txt


    2012年3月2日 上午 07:00
    版主
  • I'd like to replace $_.path with \\$servername\$path, how would one do that? Thanks

    get-content C:\serverlist.txt
    foreach-object {get-wmiobject Win32_share Path}
    2012年3月2日 下午 01:42
  • I'd like to replace $_.path with \\$servername\$path, how would one do that? Thanks

    get-content C:\serverlist.txt
    foreach-object {get-wmiobject Win32_share Path}

    Your question is too vague and does not explain wher eyou are getting $path from.

    You need to study up on what WMI is and how it works.  YOur assumptins are not close.

    Assume list of servers than all we have is the following:

    get-content C:\serverlist.txt |
         foreach-object{
                get-wmiobject Win32_share -computer $_ -filter "path='$path'"
         }

    'Path' is a property of the returned items and can only be filtered for.


    ¯\_(ツ)_/¯

    2012年3月2日 下午 01:48
  • Here is a function that you can use to report on the share size for all of your computers.

    function Get-ShareSize {
        Param(
        [String[]]$ComputerName = $env:computername
        )

    Begin{$objFldr = New-Object -com Scripting.FileSystemObject}

    Process{
        foreach($Computer in $ComputerName){
            Get-WmiObject Win32_Share -ComputerName $Computer -Filter "not name like '%$'" | %{
                $Path = $_.Path -replace 'C:',"\\$Computer\c$"
                $Size = ($objFldr.GetFolder($Path).Size) / 1GB
                New-Object PSObject -Property @{
                Name = $_.Name
                Path = $Path
                Description = $_.Description
                Size = $Size
                }
            }
        }
    }
    } Get-ShareSize -ComputerName (get-content C:\serverlist.txt)


    Rich Prescott | Infrastructure Architect, Windows Engineer and PowerShell blogger | MCITP, MCTS, MCP

    Engineering Efficiency
    @Rich_Prescott
    Windows System Administration tool
    AD User Creation tool


    2012年3月2日 下午 10:51
    版主
  • Rich - if you test very carefull I think you will find that that only works against teh local computer because $_.Path is a local path like c:\test.

    Ther eis really no good way to get share sizes except to access the admin share.  YOu can use the local path as a hint as to how to build the admin share reference.

    if path i sc:\test and computer is serv1 then

    \\server1\c$\test is the folder admin  path.

    PS>$objFldr = New-Object -com Scripting.FileSystemObje
    PS>$objFldr.GetFolder('\\ws101\c$\scripts')
    Path             : \\ws101\c$\scripts
    Name             : scripts
    ShortPath        : \\ws101\c$\scripts
    ShortName        : scripts
    Drive            : System.__ComObject
    ParentFolder     : System.__ComObject
    Attributes       : 16
    DateCreated      : 3/30/2011 1:55:24 PM
    DateLastModified : 2/15/2012 11:33:49 AM
    DateLastAccessed : 2/15/2012 11:33:49 AM
    Type             : File Folder
    IsRootFolder     : False
    Size             : 3872358
    SubFolders       : System.__ComObject
    Files            : System.__ComObject


    ¯\_(ツ)_/¯

    2012年3月2日 下午 11:17
  • Rich - if you test very carefull I think you will find that that only works against teh local computer because $_.Path is a local path like c:\test.

    Ther eis really no good way to get share sizes except to access the admin share.  YOu can use the local path as a hint as to how to build the admin share reference.

    if path i sc:\test and computer is serv1 then

    \\server1\c$\test is the folder admin  path.

    PS>$objFldr = New-Object -com Scripting.FileSystemObje
    PS>$objFldr.GetFolder('\\ws101\c$\scripts')
    Path             : \\ws101\c$\scripts
    Name             : scripts
    ShortPath        : \\ws101\c$\scripts
    ShortName        : scripts
    Drive            : System.__ComObject
    ParentFolder     : System.__ComObject
    Attributes       : 16
    DateCreated      : 3/30/2011 1:55:24 PM
    DateLastModified : 2/15/2012 11:33:49 AM
    DateLastAccessed : 2/15/2012 11:33:49 AM
    Type             : File Folder
    IsRootFolder     : False
    Size             : 3872358
    SubFolders       : System.__ComObject
    Files            : System.__ComObject


    ¯\_(ツ)_/¯

    Updated the code to reflect this.  My home lab is undergoing Windows 8 renovations, so I'm relegated to only using my local machine for the moment.

    Rich Prescott | Infrastructure Architect, Windows Engineer and PowerShell blogger | MCITP, MCTS, MCP

    Engineering Efficiency
    @Rich_Prescott
    Windows System Administration tool
    AD User Creation tool

    2012年3月2日 下午 11:36
    版主
  • I recognozed the mistake because I did the exact same thing to myself once.


    ¯\_(ツ)_/¯

    2012年3月3日 上午 12:58