 |
|
|
RUN
Last Post 02 Jul 2010 11:46 AM by Gib. 8 Replies.
|
Sort:
|
|
Prev Next |
You are not authorized to post a reply. |
|
| Author |
Messages |
 |
Gib
 New Member Posts:5

 |
| 02 Jul 2010 07:35 AM |
|
When i write a script and try to run it (F5) nothing happens... What am i doing wrong ? I am new with PS, so i have alot to learn. Thanks for ur help guys. Gib. |
|
|
|
|
PoSherLife
 Basic Member Posts:364

 |
|
Gib
 New Member Posts:5

 |
| 02 Jul 2010 07:54 AM |
|
Thank you Cruisader for your quick answer. In the first place i did not write any scripts. I copyed a script from the internet to have an idea how PS scripting works. When i understand it all, then i will try my own. Here´s the script i copyed: Now, when i click F5 then i get a message with some things i don´t understand at the bottom of the screen. I have been writing in AmigaBasic to long, so i want to try something new, and PS scriptings seems to be the thing i want. Many thanks for the help friend. GIB Set-executionpolicy Function Get-Uptime { Param([string]$server) Begin { function Uptime { param([string]$srv) $os = Get-WmiObject Win32_OperatingSystem -ComputerName $srv $uptime = $os.LastBootUpTime return $uptime } function ConvertDate { param([string]$date) $year = $date.substring(0,4) $Month = $date.Substring(4,2) $Day = $date.Substring(6,2) $hour = $date.Substring(8,2) $min = $date.Substring(10,2) $sec = $date.Substring(12,2) $RebootTime = new-Object System.DateTime($year,$month,$day,$hour,$min,$sec) $now = [System.DateTime]::Now $uptime = $now.Subtract($RebootTime) Write-Host "==> Uptime: " -NoNewLine Write-Host "$($uptime.days)" -NoNewLine -ForeGroundColor Green Write-Host " days, " -NoNewLine Write-Host "$($uptime.Hours)" -NoNewLine -ForeGroundColor Green Write-Host " hours, " -NoNewLine Write-Host "$($uptime.Minutes)" -NoNewLine -ForeGroundColor Green Write-Host " minutes, " -NoNewLine Write-Host "$($uptime.seconds)" -NoNewLine -ForeGroundColor Green Write-Host " seconds, " } Write-Host $process = @() } Process { if($_){ if($_.ServerName ){ $process += $_.ServerName } else{ $process += $_ } } } End { if($Server){$process += $server} foreach ($Server in $process){ $result = uptime $server Write-Host "Uptime for Server [" -NoNewline Write-Host $server -NoNewline -foregroundcolor Cyan Write-Host "]" ConvertDate $result Write-Host } Write-Host } } |
|
|
|
|
PoSherLife
 Basic Member Posts:364

 |
| 02 Jul 2010 08:42 AM |
|
What you posted is just a function, not a full script. First Set-ExecutionPolicy needs removed. This function also requires a parameter (Param[string]$server). So, to execute this function you need to call it like: Get-Uptime Computer_Name_Here
Copy what I have below and it will return the uptime of localhost:
### Begin Copy ###
Function Get-Uptime { Param([string]$server) Begin { function Uptime { param([string]$srv) $os = Get-WmiObject Win32_OperatingSystem -ComputerName $srv $uptime = $os.LastBootUpTime return $uptime } function ConvertDate { param([string]$date) $year = $date.substring(0,4) $Month = $date.Substring(4,2) $Day = $date.Substring(6,2) $hour = $date.Substring(8,2) $min = $date.Substring(10,2) $sec = $date.Substring(12,2) $RebootTime = new-Object System.DateTime($year,$month,$day,$hour,$min,$sec) $now = [System.DateTime]::Now $uptime = $now.Subtract($RebootTime) Write-Host "==> Uptime: " -NoNewLine Write-Host "$($uptime.days)" -NoNewLine -ForeGroundColor Green Write-Host " days, " -NoNewLine Write-Host "$($uptime.Hours)" -NoNewLine -ForeGroundColor Green Write-Host " hours, " -NoNewLine Write-Host "$($uptime.Minutes)" -NoNewLine -ForeGroundColor Green Write-Host " minutes, " -NoNewLine Write-Host "$($uptime.seconds)" -NoNewLine -ForeGroundColor Green Write-Host " seconds, " } Write-Host $process = @() } Process { if($_){ if($_.ServerName ){ $process += $_.ServerName } else{ $process += $_ } } } End { if($Server){$process += $server} foreach ($Server in $process){ $result = uptime $server Write-Host "Uptime for Server [" -NoNewline Write-Host $server -NoNewline -foregroundcolor Cyan Write-Host "]" ConvertDate $result Write-Host } Write-Host } }
Get-Uptime localhost
### End Copy ###
|
|
| When at first you don't succeed Step-Into
http://theposherlife.blogspot.com
http://www.jandctravels.com |
|
|
Gib
 New Member Posts:5

 |
| 02 Jul 2010 08:52 AM |
|
Thank you again for answering. as before i told you, i am new at PS. So i understand that when i run this function, nothing realy happens like in BASIC ? you write a script and see the result in an new window ??? |
|
|
|
|
PoSherLife
 Basic Member Posts:364

 |
| 02 Jul 2010 09:12 AM |
|
You should get an output like this in the same window: Uptime for Server [localhost] ==> Uptime: 7 days, 1 hours, 34 minutes, 49 seconds, I would recommend Richard Siddaway's book PowerShell In Practice. It is highly recommended by several MS MVP's and seems to have great content. That or the ever present Marco Shaws co-authored book "Sams Windows PowerShell Unleashed 2nd Edition" |
|
| When at first you don't succeed Step-Into
http://theposherlife.blogspot.com
http://www.jandctravels.com |
|
|
Gib
 New Member Posts:5

 |
| 02 Jul 2010 09:38 AM |
|
Thank you Cruisader :) atleast now i know what book to by on PS. But what i am looking for is "can you also program with PS" ? I want to write a new addressbook, my oldone is in Basic and to slow. And, can i also run the program withous seing the scripting on top of it all the time ? |
|
|
|
|
PoSherLife
 Basic Member Posts:364

 |
| 02 Jul 2010 09:47 AM |
|
PowerShell is not a programing language. It is a rich object oriented scripting language that is highly extensible. That being said:
There are several ways to create GUI based scripts. One highly regarded method is to use PrimalForms from SAPIEN. This tool utilizes WinForms and is great.
If you want to create an addressbook then you'll need to rely on either a DB backend (access, sql, oracle...what ever) or flat txt/csv files, and could use PrimalForms to create the front end.
|
|
| When at first you don't succeed Step-Into
http://theposherlife.blogspot.com
http://www.jandctravels.com |
|
|
Gib
 New Member Posts:5

 |
| 02 Jul 2010 11:46 AM |
|
ok, thanks again m8 :) so i´ll delete PS. its no use for me then lol back to VB and python. thanks again friend. GIB. |
|
|
|
|
| You are not authorized to post a reply. |
|
Active Forums 4.3
|
|
 |