|
|
redirecting loop output
Hey guys,
I am trying to write a little script that will check the status of the
firewall
on server names that are stored inside a text file.
I am able to loop through the names in the text file and get output onto
the screen
telling me whether or not the fire wall is active or not.
I would like to take this output though and redirect it to another text
file to be called on later.
So if the firewall is on it will go to one text file and if the
firewall is off it will output to another text file. However I have been
unsuccessfully at getting the output to redirect. Is there no capability
to do this inside a loop?
Here is what I have so far:
Code:
--------------------
foreach ($i in $servers) {
write-host -nonew $i": "
Invoke-command �computername $i �scriptblock {
$fwmgr=(new-object -com hnetcfg.fwmgr).localpolicy.currentprofile
if($fwmgr.Firewallenabled -eq $true) {
write-host "Firewall is on" -fore "green" | out-file C:\fire_on.txt
}ELSE{
write-host "Firewall if off" -fore "red" | out-file C:\fire_off.txt
}
}
}
--------------------
I was hoping I might be able to use the out-file command to redirect the
output but that doesn't seem to be working? Can anyone perhaps shed some
light on why I might be not able to do this?
Thank you
Cheers
--
toddh
|
|
0
|
|
|
|
Reply
|
toddh
|
4/14/2010 12:35:00 AM |
|
Try
"Firewall is on" |
tee-object -FilePath C:\fire_on.txt |
write-host -foreground "green"
- Larry
On 4/13/2010 7:35 PM, toddh wrote:
> Hey guys,
>
> I am trying to write a little script that will check the status of the
> firewall
> on server names that are stored inside a text file.
> I am able to loop through the names in the text file and get output onto
> the screen
> telling me whether or not the fire wall is active or not.
>
> I would like to take this output though and redirect it to another text
> file to be called on later.
> So if the firewall is on it will go to one text file and if the
> firewall is off it will output to another text file. However I have been
> unsuccessfully at getting the output to redirect. Is there no capability
> to do this inside a loop?
>
> Here is what I have so far:
>
> Code:
> --------------------
>
> foreach ($i in $servers) {
> write-host -nonew $i": "
> Invoke-command �computername $i �scriptblock {
> $fwmgr=(new-object -com hnetcfg.fwmgr).localpolicy.currentprofile
> if($fwmgr.Firewallenabled -eq $true) {
> write-host "Firewall is on" -fore "green" | out-file C:\fire_on.txt
> }ELSE{
> write-host "Firewall if off" -fore "red" | out-file C:\fire_off.txt
> }
> }
> }
>
> --------------------
>
> I was hoping I might be able to use the out-file command to redirect the
> output but that doesn't seem to be working? Can anyone perhaps shed some
> light on why I might be not able to do this?
>
|
|
0
|
|
|
|
Reply
|
Larry__Weiss
|
4/14/2010 1:06:32 AM
|
|
Hey Larry,
Thank you very much for that, it worked.
I was wondering though, how can I get the name of the server to output
with the line "firewall is *" ?
I noticed inside the text file that is being left out as well I do not
believe the information is being appended so I only get one line inside
the text file. I checked the help on the tee-object and there does not
appear to be an append option.
Thank you
Cheers
Larry__Weiss;1253947 Wrote:
> Try
>
> "Firewall is on" |
> tee-object -FilePath C:\fire_on.txt |
> write-host -foreground "green"
>
> - Larry
>
> On 4/13/2010 7:35 PM, toddh wrote:> > >
> > > Hey guys,
> > >
> > > I am trying to write a little script that will check the status of the
> > > firewall
> > > on server names that are stored inside a text file.
> > > I am able to loop through the names in the text file and get output
> > onto
> > > the screen
> > > telling me whether or not the fire wall is active or not.
> > >
> > > I would like to take this output though and redirect it to another
> > text
> > > file to be called on later.
> > > So if the firewall is on it will go to one text file and if the
> > > firewall is off it will output to another text file. However I have
> > been
> > > unsuccessfully at getting the output to redirect. Is there no
> > capability
> > > to do this inside a loop?
> > >
> > > Here is what I have so far:
> > >
> > > Code:
> > > --------------------
> > >
> > > foreach ($i in $servers) {
> > > write-host -nonew $i": "
> > > Invoke-command �computername $i �scriptblock {
> > > $fwmgr=(new-object -com hnetcfg.fwmgr).localpolicy.currentprofile
> > > if($fwmgr.Firewallenabled -eq $true) {
> > > write-host "Firewall is on" -fore "green" | out-file C:\fire_on.txt
> > > }ELSE{
> > > write-host "Firewall if off" -fore "red" | out-file C:\fire_off.txt
> > > }
> > > }
> > > }
> > >
> > > --------------------
> > >
> > > I was hoping I might be able to use the out-file command to redirect
> > the
> > > output but that doesn't seem to be working? Can anyone perhaps shed
> > some
> > > light on why I might be not able to do this?
> > > > >
--
toddh
|
|
0
|
|
|
|
Reply
|
toddh
|
4/14/2010 1:38:49 AM
|
|
That is a "problem" with Tee-Object (no append option that I know of).
So, you might have to use more that one statement:
$msg = "Firewall is on"; write-host $msg -foreground "green"; $msg >>
C:\fire_on.txt
and try this version to get the name of the server in $msg
$msg = "Firewall $i is on"; write-host $msg -foreground "green"; $msg >>
C:\fire_on.txt
See
https://connect.microsoft.com/PowerShell/feedback/details/137702/tee-object-needs-append-parameter?wa=wsignin1.0
where a request to add -Append option to Tee-Object was rejected without comment
by Microsoft.
- Larry
On 4/13/2010 8:38 PM, toddh wrote:
> Hey Larry,
>
> Thank you very much for that, it worked.
> I was wondering though, how can I get the name of the server to output
> with the line "firewall is *" ?
> I noticed inside the text file that is being left out as well I do not
> believe the information is being appended so I only get one line inside
> the text file. I checked the help on the tee-object and there does not
> appear to be an append option.
>
> Thank you
>
>
> Cheers
> Larry__Weiss;1253947 Wrote:
>> Try
>>
>> "Firewall is on" |
>> tee-object -FilePath C:\fire_on.txt |
>> write-host -foreground "green"
>>
>> - Larry
>>
>> On 4/13/2010 7:35 PM, toddh wrote:> > >
>>>> Hey guys,
>>>>
>>>> I am trying to write a little script that will check the status of the
>>>> firewall
>>>> on server names that are stored inside a text file.
>>>> I am able to loop through the names in the text file and get output
>>> onto
>>>> the screen
>>>> telling me whether or not the fire wall is active or not.
>>>>
>>>> I would like to take this output though and redirect it to another
>>> text
>>>> file to be called on later.
>>>> So if the firewall is on it will go to one text file and if the
>>>> firewall is off it will output to another text file. However I have
>>> been
>>>> unsuccessfully at getting the output to redirect. Is there no
>>> capability
>>>> to do this inside a loop?
>>>>
>>>> Here is what I have so far:
>>>>
>>>> Code:
>>>> --------------------
>>>>
>>>> foreach ($i in $servers) {
>>>> write-host -nonew $i": "
>>>> Invoke-command �computername $i �scriptblock {
>>>> $fwmgr=(new-object -com hnetcfg.fwmgr).localpolicy.currentprofile
>>>> if($fwmgr.Firewallenabled -eq $true) {
>>>> write-host "Firewall is on" -fore "green" | out-file C:\fire_on.txt
>>>> }ELSE{
>>>> write-host "Firewall if off" -fore "red" | out-file C:\fire_off.txt
>>>> }
>>>> }
>>>> }
>>>>
>>>> --------------------
>>>>
>>>> I was hoping I might be able to use the out-file command to redirect
>>> the
>>>> output but that doesn't seem to be working? Can anyone perhaps shed
>>> some
>>>> light on why I might be not able to do this?
>>>>>>
>
>
|
|
0
|
|
|
|
Reply
|
Larry__Weiss
|
4/14/2010 2:59:49 AM
|
|
hey guys,
After doing a bunch of testing I think I finally figured out how to do
it YAY :)
Code:
--------------------
write-host "Firewall status"
write-host "---------------"
$servers = get-content C:\serverlist.txt
foreach ($i in $servers) {
#write-host -nonew $i": "
Invoke-command �computername $i �scriptblock {
$fwmgr=(new-object -com hnetcfg.fwmgr).localpolicy.currentprofile
if($fwmgr.Firewallenabled -eq $true) {
$hostname= $env:COMPUTERNAME
$hostname
write-host "Firewall is on" -fore "green"
}ELSE{
$hostname= $env:COMPUTERNAME
$hostname
write-host "Firewall if off" -fore "DarkRed"
}
--------------------
Hope this can help someone else :)
Thanks for your help
Cheers
\________________________-
Ah darn, I just realized I totally forgot about the output redirection
haha :(
So it only sort of works ugh, I can't get it to put the name of the
server inside unfortunately
--
toddh
|
|
0
|
|
|
|
Reply
|
toddh
|
4/14/2010 3:19:13 AM
|
|
Try this:
$servers =3D get-content C:\serverlist.txt
foreach ($i in $servers) {
Invoke-command =96computername $i =96scriptblock {
param($computerName)
$fwmgr=3D(new-object -com
hnetcfg.fwmgr).localpolicy.currentprofile
$enabled =3D $fwmgr.Firewallenabled -eq $true
$message =3D "Firewall {0} is {1}" -f $computerName,$
(if($enabled){"On"}else{"Off"})
Write-Host $message -ForegroundColor $(if($enabled){"Green"}
else{"Red"})
$message | Add-Content C:\fire_on.txt
} -ArgumentList $i
}
This will also show you how to use $(..) and that scriptblocks in
Invoke-Command can take arguments.
You can remove the argument to Invoke-Command and just use
$env:COMPUTERNAME as you proposed.
On Apr 14, 5:19=A0am, toddh <gu...@unknown-email.com> wrote:
> hey guys,
>
> After doing a bunch of testing I think I finally figured out how to do
> it YAY :)
>
> Code:
> --------------------
>
> =A0 write-host "Firewall status"
> =A0 write-host "---------------"
> =A0 $servers =3D get-content C:\serverlist.txt
> =A0 foreach ($i in $servers) {
> =A0 #write-host -nonew $i": "
>
> =A0 Invoke-command =96computername $i =96scriptblock {
> =A0 $fwmgr=3D(new-object -com hnetcfg.fwmgr).localpolicy.currentprofile
> =A0 if($fwmgr.Firewallenabled -eq $true) {
> =A0 $hostname=3D $env:COMPUTERNAME
> =A0 $hostname
> =A0 write-host "Firewall is on" -fore "green"
> =A0 }ELSE{
> =A0 $hostname=3D $env:COMPUTERNAME
> =A0 $hostname
> =A0 write-host "Firewall if off" -fore "DarkRed"
> =A0 }
>
> --------------------
> Hope this can help someone else :)
>
> Thanks for your help
>
> Cheers
>
> \________________________-
>
> Ah darn, I just realized I totally forgot about the output redirection
> haha :(
>
> So it only sort of works ugh, I can't get it to put the name of the
> server inside unfortunately
>
> --
> toddh
|
|
0
|
|
|
|
Reply
|
stej
|
4/14/2010 8:48:14 AM
|
|
Try this
if (test-path firewallon.txt){Remove-Item firewallon.txt}
if (test-path firewalloff.txt){Remove-Item firewalloff.txt}
write-host "Firewall status"
write-host "---------------"
$servers = get-content C:\serverlist.txt
foreach ($i in $servers) {
#write-host -nonew $i": "
Invoke-command –computername $i –scriptblock {
$fwmgr=(new-object -com hnetcfg.fwmgr).localpolicy.currentprofile
if($fwmgr.Firewallenabled -eq $true) {
$on = "$($env:COMPUTERNAME) Firewall is on"
Add-Content -Value $on -Path firewallon.txt
write-host $on -fore "green"
}
ELSE {
$off = "$($env:COMPUTERNAME) Firewall is off"
Add-Content -Value $off -Path firewalloff.txt
write-host $off -fore "DarkRed"
}
}
--
Richard Siddaway
All scripts are supplied "as is" and with no warranty
PowerShell MVP
Blog and PowerShell User Group:
http://msmvps.com/blogs/RichardSiddaway/Default.aspx
"toddh" wrote:
>
> hey guys,
>
> After doing a bunch of testing I think I finally figured out how to do
> it YAY :)
>
> Code:
> --------------------
>
> write-host "Firewall status"
> write-host "---------------"
> $servers = get-content C:\serverlist.txt
> foreach ($i in $servers) {
> #write-host -nonew $i": "
>
> Invoke-command –computername $i –scriptblock {
> $fwmgr=(new-object -com hnetcfg.fwmgr).localpolicy.currentprofile
> if($fwmgr.Firewallenabled -eq $true) {
> $hostname= $env:COMPUTERNAME
> $hostname
> write-host "Firewall is on" -fore "green"
> }ELSE{
> $hostname= $env:COMPUTERNAME
> $hostname
> write-host "Firewall if off" -fore "DarkRed"
> }
>
> --------------------
> Hope this can help someone else :)
>
> Thanks for your help
>
>
> Cheers
>
> \________________________-
>
>
> Ah darn, I just realized I totally forgot about the output redirection
> haha :(
>
> So it only sort of works ugh, I can't get it to put the name of the
> server inside unfortunately
>
>
> --
> toddh
> .
>
|
|
0
|
|
|
|
Reply
|
Utf
|
4/14/2010 9:01:01 AM
|
|
|
6 Replies
241 Views
(page loaded in 0.338 seconds)
|
|
|
|
|
|
|
|
|