tdodd72
asked on
Powershell array 2 questions
Greetings I have this script and many thanks to those who helped. I have two things I would like to accomplish. I have multiple fileshares for EX: \\fileshare\share1 \\fileshare\share2 \\fileshare\share3. I am confused as to who I would incorporate this in my script. It would be time consuming to hard code this every time I run this.
Does anyone have a tutorial on data being put in an excel sheet. I would like to learn this but hte examples I am seeing are either too basic or advance and do not provide enough info.
"*.txt","*.log", "*.mp3", "*.avi", "*.rar" | foreach {
$( $Files = Get-ChildItem "\\fileshare\share" -Recurse -Filter $_
$Files | %{ "$($_.FullName) - $($_.Length/1Kb)Kb" } >> c:\temp\log.dat
""
"Total Number of Files: $(([Array]$Files).Count)"
"Total Size: $(($Files | Measure-Object -Sum Length).Sum/1Gb)Gb" )
}
Does anyone have a tutorial on data being put in an excel sheet. I would like to learn this but hte examples I am seeing are either too basic or advance and do not provide enough info.
"*.txt","*.log", "*.mp3", "*.avi", "*.rar" | foreach {
$( $Files = Get-ChildItem "\\fileshare\share" -Recurse -Filter $_
$Files | %{ "$($_.FullName) - $($_.Length/1Kb)Kb" } >> c:\temp\log.dat
""
"Total Number of Files: $(([Array]$Files).Count)"
"Total Size: $(($Files | Measure-Object -Sum Length).Sum/1Gb)Gb" )
}
I could write it all in here, but better yet I'll provide you with a link.
Take a look at http://powershell.com/cs/blogs/ebook/archive/2009/03/30/chapter-15-the-file-system.aspx#processing-comma-separated-lists
And in general, look stuff up in this guide. It helped me a heap when I was learning the syntax. Great website.
Take a look at http://powershell.com/cs/blogs/ebook/archive/2009/03/30/chapter-15-the-file-system.aspx#processing-comma-separated-lists
And in general, look stuff up in this guide. It helped me a heap when I was learning the syntax. Great website.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
btw that should have read:
#Whichever way you choose you should NOW be able to do a foreach
#against it.
#Whichever way you choose you should NOW be able to do a foreach
#against it.
I modified your example to give you the information you wanted.
Basically, its getting all the shares that dont match $ into an array
Then
its doing a foreach loop on the array, converting the 'name' property of the share into a string, then putting it all together in a string "\\servername\sharename" to give you the correct path outputted to the screen.
Basically, its getting all the shares that dont match $ into an array
Then
its doing a foreach loop on the array, converting the 'name' property of the share into a string, then putting it all together in a string "\\servername\sharename" to give you the correct path outputted to the screen.
$Servername = Read-host "Typer the server name here please"
"Collecting share data for: $Servername"
$GetShare = gwmi win32_Share -computername $Servername | where {$_.name -notlike "*$"}
Foreach ($Share in $GetShare) {
$Name = $Share.Name.ToString()
"\\$Servername\$Name"
}
ASKER
I added a portion in the script below and it is looking in my c:\users drive and saying it does not exist.I think I probably should have setup another array but not for sure. The variable named $name displays the correct share name but it appears it is not getting populated below in the get-childitem statement. It is saying the following when I run this script below. Can someone please help.
Also is there a way to display the output if it is under a gigabyte in megabyte form and if it is over a gigabyte to display this in GB form, thanks for all your help.
"Get-ChildItem : Cannot find path 'C:\Users\username' because it does not exist.
At line:19 char:26
+ $( $Files = Get-ChildItem <<<< "$Name" -Recurse -Filter $_"
$Servername = Read-host "Typer the server name here please"
"Collecting share data for: $Servername"
$GetShare = gwmi win32_Share -computername $Servername | where {$_.name -notlike "*$"}
Foreach ($Share in $GetShare) {
$Name = $Share.Name.ToString()
"\\$Servername\$Name"
}
Foreach ($Server in $Name)
{
"*.txt","*.log", "*.mp3", "*.avi", "*.rar" | foreach {
$( $Files = Get-ChildItem "$Name" -Recurse -Filter $_
$Files | %{ "$($_.FullName) - $($_.Length/1Kb)Kb" } >> c:\temp\log.dat
""
"Total Number of Files: $(([Array]$Files).Count)"
"Total Size: $(($Files | Measure-Object -Sum Length).Sum/1Gb)Gb" )
}
}
Also is there a way to display the output if it is under a gigabyte in megabyte form and if it is over a gigabyte to display this in GB form, thanks for all your help.
"Get-ChildItem : Cannot find path 'C:\Users\username' because it does not exist.
At line:19 char:26
+ $( $Files = Get-ChildItem <<<< "$Name" -Recurse -Filter $_"
$Servername = Read-host "Typer the server name here please"
"Collecting share data for: $Servername"
$GetShare = gwmi win32_Share -computername $Servername | where {$_.name -notlike "*$"}
Foreach ($Share in $GetShare) {
$Name = $Share.Name.ToString()
"\\$Servername\$Name"
}
Foreach ($Server in $Name)
{
"*.txt","*.log", "*.mp3", "*.avi", "*.rar" | foreach {
$( $Files = Get-ChildItem "$Name" -Recurse -Filter $_
$Files | %{ "$($_.FullName) - $($_.Length/1Kb)Kb" } >> c:\temp\log.dat
""
"Total Number of Files: $(([Array]$Files).Count)"
"Total Size: $(($Files | Measure-Object -Sum Length).Sum/1Gb)Gb" )
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hmm, I have tried to run the code and keep getting this error message
Missing closing '}' in statement block.
At line:14 char:66
Unexpected token ')' in expression or statement.
At line:14 char:66
I have placed those missing characters and verified but still cannot run. I am using powershell v2.0 ISE.
Missing closing '}' in statement block.
At line:14 char:66
Unexpected token ')' in expression or statement.
At line:14 char:66
I have placed those missing characters and verified but still cannot run. I am using powershell v2.0 ISE.
Fixed it for ya. I also added the GB and MB switch that you talked about earlier.
$Servername = Read-host "Typer the server name here please"
$Extensions = "*.txt","*.log", "*.mp3", "*.avi", "*.rar"
"Collecting share data for: $Servername"
$GetShare = gwmi win32_Share -computername $Servername | where {$_.name -notlike "*$"}
Foreach ($Share in $GetShare) {
$Name = $Share.Name.ToString()
"\\$Servername\$Name"
$Files = Get-ChildItem "\\$Servername\$Name" -Recure -Filter $Extensions
$Files | %{ "$($_.FullName) - $($_.Length/1Kb)Kb" } >> c:\temp\log.dat
""
"Total Number of Files: $(([Array]$Files).Count)"
$Size = ($Files | Measure-Object -sum Length).Sum
If ($Size -lt 1073741824) {
$SizeOutput = "{0:N2}" -f ($size / 1MB) + " MB"
}
Else {
$SizeOutput = "{0:N2}" -f ($size / 1GB) + " GB"
}
"Total Size: $SizeOutput"
}
ASKER
.
ASKER
$servername = read-host "type the server name here please"
foo $servername
$servername
$getshare = Wmiobject win32_share -computername $servername | where {$_.name -notlike "*$"} | sort-object -property path | ft name -autosize
$getshare
This gives me the name of all the fileshares on a server where I am prompted to put in. I thought about setting up an empty array to extract the share names but need to present them as \\$servername\$getshare. This is where my powershell skills are limited. Can someone please help. I just want to be able to take the output from my statement here and present it to my script I originally posted and have it to loop through.