Discussion:
PHP and Powershell
Alan Loos
2013-07-18 17:01:42 UTC
Permalink
Good morning everyone,
First time posting in here, although I've been listening in for a few weeks now.
So this one has got me stumped, I am fairly new to PHP but I cannot seem to Google through this one.
I cannot figure out how to 'exclude' PSComputerName and RunspaceId, which is ultimately what I'm struggling with. Please see below for script snips and explanations.

Also if there are any best practices you would recommend I'm open to it being that I am fairly new and self-taught to PHP scripting.


I have a bit of code I've put together (as ugly as it is) as follows in line:
<?php


###############
## Variables ##
###############

$TargetName = "Target1";
$login = "\$cred = New-Object System.Management.Automation.PSCredential -ArgumentList @('***@widget.com',(ConvertTo-SecureString -String 'MyPassword' -AsPlainText -Force))";
$command = "Invoke-Command -computername 127.0.0.1 -credential \$cred -scriptblock {& Get-IscsiServerTarget -TargetName " . $TargetName . " | % { \$_.TargetIqn, \$_.Status}} -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)";
$psCMD = "powershell -ExecutionPolicy Unrestricted -command \"$login; $command\" <NUL";


########################################
## Variable Checking (For Debug Mode) ##
########################################

#echo "\$psCMD = $psCMD";


################
## Run Script ##
################

exec($psCMD,$out);


############
## Output ##
############

echo ('<pre>');
print_r($out);
echo ('</pre>');


###################
## End Of Script ##
###################

echo "End Of Scene";

?>

The issue I have is that it feeds back:

Array
(
[0] => iqn.2013-04.com.widget:Target1
[1] =>
[2] => PSComputerName RunspaceId Value
[3] => -------------- ---------- -----
[4] => 127.0.0.1 52fb8b1b-8d8b-4eec-9419... NotConnected
[5] =>
[6] =>
)

End Of Scene

What I should see so I can then turn it into variables is what I run when I run the command straight through the local Powershell command prompt which would return:
PS C:\Users\administrator.WIDGET.000> Get-IscsiServerTarget | % { $_.TargetIqn, $_.Status}
iqn.2013-04.com.widget:Target1
NotConnected
iqn.2013-04.com.widget:Target2
NotConnected
iqn.2013-04.com.widget:Target3
NotConnected
iqn.2013-04.com.widget:Target4
Connected
iqn.2013-04.com.widget:Target5
NotConnected
iqn.2013-04.com.widget:Target6
NotConnected

I cannot figure out how to 'exclude' PSComputerName and RunspaceId

Sorry in advance for the wordy explanation.

Cheers!

Alan


Please make note of my new email address: ***@genco.com.

CONFIDENTIALITY NOTICE: This e-mail and the attachment(s) hereto (if any) contain confidential information that is privileged and intended only for the addressee(s) hereof. If you are not an intended recipient, you are hereby notified that any disclosure, copying, distribution or use of this e-mail and/or the accompanying attachment(s) is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender by return e-mail.
Serge Fonville
2013-07-19 08:05:16 UTC
Permalink
Hi,

Although this is more powershell related than PHP...

When Powershell returns an object, you can pipe the output through
Select-Object to get only certain object properties.

To better answer your question:
First, why do you specify ComputerName as 127.0.0.1 if the credential is
already specified?
Also, perhaps it is easier to create a .ps1 file that you run, especially
for readability.

HTH

Wh

Kind regards/met vriendelijke groet,

Serge Fonville

http://www.sergefonville.nl

Convince Microsoft!
They need to add TRUNCATE PARTITION in SQL Server
https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table
Post by Alan Loos
Good morning everyone,
First time posting in here, although I've been listening in for a few weeks now.
So this one has got me stumped, I am fairly new to PHP but I cannot seem
to Google through this one.
I cannot figure out how to 'exclude' PSComputerName and RunspaceId, which
is ultimately what I'm struggling with. Please see below for script snips
and explanations.
Also if there are any best practices you would recommend I'm open to it
being that I am fairly new and self-taught to PHP scripting.
<?php
###############
## Variables ##
###############
$TargetName = "Target1";
$login = "\$cred = New-Object System.Management.Automation.PSCredential
-String 'MyPassword' -AsPlainText -Force))";
$command = "Invoke-Command -computername 127.0.0.1 -credential \$cred
-scriptblock {& Get-IscsiServerTarget -TargetName " . $TargetName . " | % {
\$_.TargetIqn, \$_.Status}} -SessionOption (New-PSSessionOption
-SkipCACheck -SkipCNCheck -SkipRevocationCheck)";
$psCMD = "powershell -ExecutionPolicy Unrestricted -command \"$login; $command\" <NUL";
########################################
## Variable Checking (For Debug Mode) ##
########################################
#echo "\$psCMD = $psCMD";
################
## Run Script ##
################
exec($psCMD,$out);
############
## Output ##
############
echo ('<pre>');
print_r($out);
echo ('</pre>');
###################
## End Of Script ##
###################
echo "End Of Scene";
?>
Array
(
[0] => iqn.2013-04.com.widget:Target1
[1] =>
[2] => PSComputerName RunspaceId Value
[3] => -------------- ---------- -----
[4] => 127.0.0.1 52fb8b1b-8d8b-4eec-9419... NotConnected
[5] =>
[6] =>
)
End Of Scene
What I should see so I can then turn it into variables is what I run when
I run the command straight through the local Powershell command prompt
PS C:\Users\administrator.WIDGET.000> Get-IscsiServerTarget | % { $_.TargetIqn, $_.Status}
iqn.2013-04.com.widget:Target1
NotConnected
iqn.2013-04.com.widget:Target2
NotConnected
iqn.2013-04.com.widget:Target3
NotConnected
iqn.2013-04.com.widget:Target4
Connected
iqn.2013-04.com.widget:Target5
NotConnected
iqn.2013-04.com.widget:Target6
NotConnected
I cannot figure out how to 'exclude' PSComputerName and RunspaceId
Sorry in advance for the wordy explanation.
Cheers!
Alan
CONFIDENTIALITY NOTICE: This e-mail and the attachment(s) hereto (if any)
contain confidential information that is privileged and intended only for
the addressee(s) hereof. If you are not an intended recipient, you are
hereby notified that any disclosure, copying, distribution or use of this
e-mail and/or the accompanying attachment(s) is strictly prohibited. If you
have received this e-mail in error, please immediately notify the sender by
return e-mail.
Alan Loos
2013-07-19 14:57:15 UTC
Permalink
Thank you for your response Serge!
The computer name is for the invoke-command, ComputerName has to be specified to tell the computer where the command is to be routed. Since I want this to run locally over PHP I figured that it would be a good way to avoid running a script as I have read online that when it can be avoided it's a good practice (I also couldn't get this to work properly... :) But that is a side note). The piece that outputs the extra RunSpaceID and the PSComputerName is the $_.Status portion of everything.

If someone knows how to cut it out that would be fine but really I just need to pull the Target Name and the Value from result. For the attached example it would be $IQNTarget = iqn.2013-04.com.widget:Target1 and $Value = NotConnected (Which there are two options for this which is Connected or NotConnected).

To use Select Object here would I do Select-Object $Value?

Alan Loos | Flash Anywhere Project
T 925-640-2977 | ***@genco.com<mailto:***@genco.com>

CONFIDENTIALITY NOTICE: This e-mail and the attachment(s) hereto (if any) contain confidential information that is privileged and intended only for the addressee(s) hereof. If you are not an intended recipient, you are hereby notified that any disclosure, copying, distribution or use of this e-mail and/or the accompanying attachment(s) is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender by return e-mail.

From: Serge Fonville [mailto:***@gmail.com]
Sent: Friday, July 19, 2013 3:05 AM
To: Alan Loos
Cc: php-***@lists.php.net
Subject: Re: [PHP] PHP and Powershell

Hi,

Although this is more powershell related than PHP...

When Powershell returns an object, you can pipe the output through Select-Object to get only certain object properties.

To better answer your question:
First, why do you specify ComputerName as 127.0.0.1 if the credential is already specified?
Also, perhaps it is easier to create a .ps1 file that you run, especially for readability.

HTH

Wh

Kind regards/met vriendelijke groet,

Serge Fonville

http://www.sergefonville.nl

Convince Microsoft!
They need to add TRUNCATE PARTITION in SQL Server
https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table

2013/7/18 Alan Loos <***@genco.com<mailto:***@genco.com>>
Good morning everyone,
First time posting in here, although I've been listening in for a few weeks now.
So this one has got me stumped, I am fairly new to PHP but I cannot seem to Google through this one.
I cannot figure out how to 'exclude' PSComputerName and RunspaceId, which is ultimately what I'm struggling with. Please see below for script snips and explanations.

Also if there are any best practices you would recommend I'm open to it being that I am fairly new and self-taught to PHP scripting.


I have a bit of code I've put together (as ugly as it is) as follows in line:
<?php


###############
## Variables ##
###############

$TargetName = "Target1";
$login = "\$cred = New-Object System.Management.Automation.PSCredential -ArgumentList @('***@widget.com<mailto:***@widget.com>',(ConvertTo-SecureString -String 'MyPassword' -AsPlainText -Force))";
$command = "Invoke-Command -computername 127.0.0.1 -credential \$cred -scriptblock {& Get-IscsiServerTarget -TargetName " . $TargetName . " | % { \$_.TargetIqn, \$_.Status}} -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)";
$psCMD = "powershell -ExecutionPolicy Unrestricted -command \"$login; $command\" <NUL";


########################################
## Variable Checking (For Debug Mode) ##
########################################

#echo "\$psCMD = $psCMD";


################
## Run Script ##
################

exec($psCMD,$out);


############
## Output ##
############

echo ('<pre>');
print_r($out);
echo ('</pre>');


###################
## End Of Script ##
###################

echo "End Of Scene";

?>

The issue I have is that it feeds back:

Array
(
[0] => iqn.2013-04.com.widget:Target1
[1] =>
[2] => PSComputerName RunspaceId Value
[3] => -------------- ---------- -----
[4] => 127.0.0.1 52fb8b1b-8d8b-4eec-9419... NotConnected
[5] =>
[6] =>
)

End Of Scene

What I should see so I can then turn it into variables is what I run when I run the command straight through the local Powershell command prompt which would return:
PS C:\Users\administrator.WIDGET.000> Get-IscsiServerTarget | % { $_.TargetIqn, $_.Status}
iqn.2013-04.com.widget:Target1
NotConnected
iqn.2013-04.com.widget:Target2
NotConnected
iqn.2013-04.com.widget:Target3
NotConnected
iqn.2013-04.com.widget:Target4
Connected
iqn.2013-04.com.widget:Target5
NotConnected
iqn.2013-04.com.widget:Target6
NotConnected

I cannot figure out how to 'exclude' PSComputerName and RunspaceId

Sorry in advance for the wordy explanation.

Cheers!

Alan


Please make note of my new email address: ***@genco.com<mailto:***@genco.com>.

CONFIDENTIALITY NOTICE: This e-mail and the attachment(s) hereto (if any) contain confidential information that is privileged and intended only for the addressee(s) hereof. If you are not an intended recipient, you are hereby notified that any disclosure, copying, distribution or use of this e-mail and/or the accompanying attachment(s) is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender by return e-mail.
Serge Fonville
2013-07-19 16:12:52 UTC
Permalink
How have you determined that invoke-command requires -computername?

Kind regards/met vriendelijke groet,

Serge Fonville

http://www.sergefonville.nl

Convince Microsoft!
They need to add TRUNCATE PARTITION in SQL Server
https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table
Thank you for your response Serge!****
The computer name is for the invoke-command, ComputerName has to be
specified to tell the computer where the command is to be routed. Since I
want this to run locally over PHP I figured that it would be a good way to
avoid running a script as I have read online that when it can be avoided
it’s a good practice (I also couldn’t get this to work properly… J But
that is a side note). The piece that outputs the extra RunSpaceID and the
PSComputerName is the $_.Status portion of everything.****
** **
If someone knows how to cut it out that would be fine but really I just
need to pull the Target Name and the Value from result. For the attached
example it would be $IQNTarget = iqn.2013-04.com.widget:Target1 and $Value
= NotConnected (Which there are two options for this which is Connected or
NotConnected).****
** **
To use Select Object here would I do Select-Object $Value?****
** **
*Alan Loos* | *Flash Anywhere Project*****
** **
CONFIDENTIALITY NOTICE: This e-mail and the attachment(s) hereto (if any)
contain confidential information that is privileged and intended only for
the addressee(s) hereof. If you are not an intended recipient, you are
hereby notified that any disclosure, copying, distribution or use of this
e-mail and/or the accompanying attachment(s) is strictly prohibited. If you
have received this e-mail in error, please immediately notify the sender by
return e-mail.****
** **
*Sent:* Friday, July 19, 2013 3:05 AM
*To:* Alan Loos
*Subject:* Re: [PHP] PHP and Powershell****
** **
Hi,
Although this is more powershell related than PHP...
When Powershell returns an object, you can pipe the output through
Select-Object to get only certain object properties.
First, why do you specify ComputerName as 127.0.0.1 if the credential is already specified?
Also, perhaps it is easier to create a .ps1 file that you run, especially for readability.
HTH
Wh
****
Kind regards/met vriendelijke groet,****
** **
Serge Fonville****
** **
http://www.sergefonville.nl
Convince Microsoft!****
They need to add TRUNCATE PARTITION in SQL Server****
https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table
****
** **
Good morning everyone,
First time posting in here, although I've been listening in for a few weeks now.
So this one has got me stumped, I am fairly new to PHP but I cannot seem
to Google through this one.
I cannot figure out how to 'exclude' PSComputerName and RunspaceId, which
is ultimately what I'm struggling with. Please see below for script snips
and explanations.
Also if there are any best practices you would recommend I'm open to it
being that I am fairly new and self-taught to PHP scripting.
<?php
###############
## Variables ##
###############
$TargetName = "Target1";
$login = "\$cred = New-Object System.Management.Automation.PSCredential
-String 'MyPassword' -AsPlainText -Force))";
$command = "Invoke-Command -computername 127.0.0.1 -credential \$cred
-scriptblock {& Get-IscsiServerTarget -TargetName " . $TargetName . " | % {
\$_.TargetIqn, \$_.Status}} -SessionOption (New-PSSessionOption
-SkipCACheck -SkipCNCheck -SkipRevocationCheck)";
$psCMD = "powershell -ExecutionPolicy Unrestricted -command \"$login; $command\" <NUL";
########################################
## Variable Checking (For Debug Mode) ##
########################################
#echo "\$psCMD = $psCMD";
################
## Run Script ##
################
exec($psCMD,$out);
############
## Output ##
############
echo ('<pre>');
print_r($out);
echo ('</pre>');
###################
## End Of Script ##
###################
echo "End Of Scene";
?>
Array
(
[0] => iqn.2013-04.com.widget:Target1
[1] =>
[2] => PSComputerName RunspaceId Value
[3] => -------------- ---------- -----
[4] => 127.0.0.1 52fb8b1b-8d8b-4eec-9419... NotConnected
[5] =>
[6] =>
)
End Of Scene
What I should see so I can then turn it into variables is what I run when
I run the command straight through the local Powershell command prompt
PS C:\Users\administrator.WIDGET.000> Get-IscsiServerTarget | % { $_.TargetIqn, $_.Status}
iqn.2013-04.com.widget:Target1
NotConnected
iqn.2013-04.com.widget:Target2
NotConnected
iqn.2013-04.com.widget:Target3
NotConnected
iqn.2013-04.com.widget:Target4
Connected
iqn.2013-04.com.widget:Target5
NotConnected
iqn.2013-04.com.widget:Target6
NotConnected
I cannot figure out how to 'exclude' PSComputerName and RunspaceId
Sorry in advance for the wordy explanation.
Cheers!
Alan
CONFIDENTIALITY NOTICE: This e-mail and the attachment(s) hereto (if any)
contain confidential information that is privileged and intended only for
the addressee(s) hereof. If you are not an intended recipient, you are
hereby notified that any disclosure, copying, distribution or use of this
e-mail and/or the accompanying attachment(s) is strictly prohibited. If you
have received this e-mail in error, please immediately notify the sender by
return e-mail.****
** **
Alan Loos
2013-07-19 16:18:33 UTC
Permalink
According to Technet it is not required (http://technet.microsoft.com/en-us/library/hh849719.aspx) as the default would be 'localhost' which is the same as 127.0.0.1.

So you are correct it is not required. However when I take away '-computername 127.0.0.1' I get a return of:
Array
(
[0] => Invoke-Command : Parameter set cannot be resolved using the specified named
[1] => parameters.
[2] => At line:1 char:172
[3] => + $cred = New-Object System.Management.Automation.PSCredential -ArgumentList
[4] => @('ad ...
[5] => + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[6] => ~~~
[7] => + CategoryInfo : InvalidArgument: (:) [Invoke-Command], Parameter
[8] => BindingException
[9] => + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Comma
[10] => nds.InvokeCommandCommand
[11] =>
)

End Of Scene

For whatever reason, the powershell command does not want to execute properly without that variable being defined explicitly.

(Not sure if that is what you are asking exactly but I think that was answering the question, if not let me know and I'll try to answer the best I can)

Thanks in advance!

Alan Loos

CONFIDENTIALITY NOTICE: This e-mail and the attachment(s) hereto (if any) contain confidential information that is privileged and intended only for the addressee(s) hereof. If you are not an intended recipient, you are hereby notified that any disclosure, copying, distribution or use of this e-mail and/or the accompanying attachment(s) is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender by return e-mail.

From: Serge Fonville [mailto:***@gmail.com]
Sent: Friday, July 19, 2013 11:13 AM
To: Alan Loos
Cc: php-***@lists.php.net
Subject: Re: [PHP] PHP and Powershell

How have you determined that invoke-command requires -computername?

Kind regards/met vriendelijke groet,

Serge Fonville

http://www.sergefonville.nl

Convince Microsoft!
They need to add TRUNCATE PARTITION in SQL Server
https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table

2013/7/19 Alan Loos <***@genco.com<mailto:***@genco.com>>
Thank you for your response Serge!
The computer name is for the invoke-command, ComputerName has to be specified to tell the computer where the command is to be routed. Since I want this to run locally over PHP I figured that it would be a good way to avoid running a script as I have read online that when it can be avoided it's a good practice (I also couldn't get this to work properly... :) But that is a side note). The piece that outputs the extra RunSpaceID and the PSComputerName is the $_.Status portion of everything.

If someone knows how to cut it out that would be fine but really I just need to pull the Target Name and the Value from result. For the attached example it would be $IQNTarget = iqn.2013-04.com.widget:Target1 and $Value = NotConnected (Which there are two options for this which is Connected or NotConnected).

To use Select Object here would I do Select-Object $Value?

Alan Loos

CONFIDENTIALITY NOTICE: This e-mail and the attachment(s) hereto (if any) contain confidential information that is privileged and intended only for the addressee(s) hereof. If you are not an intended recipient, you are hereby notified that any disclosure, copying, distribution or use of this e-mail and/or the accompanying attachment(s) is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender by return e-mail.

From: Serge Fonville [mailto:***@gmail.com<mailto:***@gmail.com>]
Sent: Friday, July 19, 2013 3:05 AM
To: Alan Loos
Cc: php-***@lists.php.net<mailto:php-***@lists.php.net>
Subject: Re: [PHP] PHP and Powershell

Hi,

Although this is more powershell related than PHP...

When Powershell returns an object, you can pipe the output through Select-Object to get only certain object properties.

To better answer your question:
First, why do you specify ComputerName as 127.0.0.1 if the credential is already specified?
Also, perhaps it is easier to create a .ps1 file that you run, especially for readability.

HTH

Wh

Kind regards/met vriendelijke groet,

Serge Fonville

http://www.sergefonville.nl

Convince Microsoft!
They need to add TRUNCATE PARTITION in SQL Server
https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table

2013/7/18 Alan Loos <***@genco.com<mailto:***@genco.com>>
Good morning everyone,
First time posting in here, although I've been listening in for a few weeks now.
So this one has got me stumped, I am fairly new to PHP but I cannot seem to Google through this one.
I cannot figure out how to 'exclude' PSComputerName and RunspaceId, which is ultimately what I'm struggling with. Please see below for script snips and explanations.

Also if there are any best practices you would recommend I'm open to it being that I am fairly new and self-taught to PHP scripting.


I have a bit of code I've put together (as ugly as it is) as follows in line:
<?php


###############
## Variables ##
###############

$TargetName = "Target1";
$login = "\$cred = New-Object System.Management.Automation.PSCredential -ArgumentList @('***@widget.com<mailto:***@widget.com>',(ConvertTo-SecureString -String 'MyPassword' -AsPlainText -Force))";
$command = "Invoke-Command -computername 127.0.0.1 -credential \$cred -scriptblock {& Get-IscsiServerTarget -TargetName " . $TargetName . " | % { \$_.TargetIqn, \$_.Status}} -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)";
$psCMD = "powershell -ExecutionPolicy Unrestricted -command \"$login; $command\" <NUL";


########################################
## Variable Checking (For Debug Mode) ##
########################################

#echo "\$psCMD = $psCMD";


################
## Run Script ##
################

exec($psCMD,$out);


############
## Output ##
############

echo ('<pre>');
print_r($out);
echo ('</pre>');


###################
## End Of Script ##
###################

echo "End Of Scene";

?>

The issue I have is that it feeds back:

Array
(
[0] => iqn.2013-04.com.widget:Target1
[1] =>
[2] => PSComputerName RunspaceId Value
[3] => -------------- ---------- -----
[4] => 127.0.0.1 52fb8b1b-8d8b-4eec-9419... NotConnected
[5] =>
[6] =>
)

End Of Scene

What I should see so I can then turn it into variables is what I run when I run the command straight through the local Powershell command prompt which would return:
PS C:\Users\administrator.WIDGET.000> Get-IscsiServerTarget | % { $_.TargetIqn, $_.Status}
iqn.2013-04.com.widget:Target1
NotConnected
iqn.2013-04.com.widget:Target2
NotConnected
iqn.2013-04.com.widget:Target3
NotConnected
iqn.2013-04.com.widget:Target4
Connected
iqn.2013-04.com.widget:Target5
NotConnected
iqn.2013-04.com.widget:Target6
NotConnected

I cannot figure out how to 'exclude' PSComputerName and RunspaceId

Sorry in advance for the wordy explanation.

Cheers!

Alan


Please make note of my new email address: ***@genco.com<mailto:***@genco.com>.

CONFIDENTIALITY NOTICE: This e-mail and the attachment(s) hereto (if any) contain confidential information that is privileged and intended only for the addressee(s) hereof. If you are not an intended recipient, you are hereby notified that any disclosure, copying, distribution or use of this e-mail and/or the accompanying attachment(s) is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender by return e-mail.
Serge Fonville
2013-07-19 17:16:06 UTC
Permalink
Thank you for your clarification.

Have you considered placing the whole powershell -command parameter in a
.ps1 script and executing that instead?

The benefit would be that it is easier to read and test accordingly.

HTH

Kind regards/met vriendelijke groet,

Serge Fonville

http://www.sergefonville.nl

Convince Microsoft!
They need to add TRUNCATE PARTITION in SQL Server
https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table
Post by Alan Loos
According to Technet it is not required (
http://technet.microsoft.com/en-us/library/hh849719.aspx) as the default
would be ‘localhost’ which is the same as 127.0.0.1.****
** **
So you are correct it is not required. However when I take away
‘-computername 127.0.0.1’ I get a return of:****
Array****
(****
[0] => Invoke-Command : Parameter set cannot be resolved using the
specified named****
[1] => parameters.****
[2] => At line:1 char:172****
[3] => + $cred = New-Object System.Management.Automation.PSCredential
-ArgumentList****
[5] => +
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
****
[6] => ~~~****
[7] => + CategoryInfo : InvalidArgument: (:)
[Invoke-Command], Parameter****
[8] => BindingException****
AmbiguousParameterSet,Microsoft.PowerShell.Comma****
[10] => nds.InvokeCommandCommand****
[11] => ****
)****
** **
End Of Scene****
** **
For whatever reason, the powershell command does not want to execute
properly without that variable being defined explicitly.****
** **
(Not sure if that is what you are asking exactly but I think that was
answering the question, if not let me know and I’ll try to answer the best
I can)****
** **
Thanks in advance!****
** **
*Alan Loos* ****
** **
CONFIDENTIALITY NOTICE: This e-mail and the attachment(s) hereto (if any)
contain confidential information that is privileged and intended only for
the addressee(s) hereof. If you are not an intended recipient, you are
hereby notified that any disclosure, copying, distribution or use of this
e-mail and/or the accompanying attachment(s) is strictly prohibited. If you
have received this e-mail in error, please immediately notify the sender by
return e-mail.****
** **
*Sent:* Friday, July 19, 2013 11:13 AM
*To:* Alan Loos
*Subject:* Re: [PHP] PHP and Powershell****
** **
How have you determined that invoke-command requires -computername?
****
Kind regards/met vriendelijke groet,****
** **
Serge Fonville****
** **
http://www.sergefonville.nl
Convince Microsoft!****
They need to add TRUNCATE PARTITION in SQL Server****
https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table
****
** **
Thank you for your response Serge!****
The computer name is for the invoke-command, ComputerName has to be
specified to tell the computer where the command is to be routed. Since I
want this to run locally over PHP I figured that it would be a good way to
avoid running a script as I have read online that when it can be avoided
it’s a good practice (I also couldn’t get this to work properly… J But
that is a side note). The piece that outputs the extra RunSpaceID and the
PSComputerName is the $_.Status portion of everything.****
****
If someone knows how to cut it out that would be fine but really I just
need to pull the Target Name and the Value from result. For the attached
example it would be $IQNTarget = iqn.2013-04.com.widget:Target1 and $Value
= NotConnected (Which there are two options for this which is Connected or
NotConnected).****
****
To use Select Object here would I do Select-Object $Value?****
****
*Alan Loos*****
****
CONFIDENTIALITY NOTICE: This e-mail and the attachment(s) hereto (if any)
contain confidential information that is privileged and intended only for
the addressee(s) hereof. If you are not an intended recipient, you are
hereby notified that any disclosure, copying, distribution or use of this
e-mail and/or the accompanying attachment(s) is strictly prohibited. If you
have received this e-mail in error, please immediately notify the sender by
return e-mail.****
****
*Sent:* Friday, July 19, 2013 3:05 AM
*To:* Alan Loos
*Subject:* Re: [PHP] PHP and Powershell****
****
Hi,
Although this is more powershell related than PHP...
When Powershell returns an object, you can pipe the output through
Select-Object to get only certain object properties.
First, why do you specify ComputerName as 127.0.0.1 if the credential is already specified?
Also, perhaps it is easier to create a .ps1 file that you run, especially for readability.
HTH
Wh
****
Kind regards/met vriendelijke groet,****
****
Serge Fonville****
****
http://www.sergefonville.nl
Convince Microsoft!****
They need to add TRUNCATE PARTITION in SQL Server****
https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table
****
****
Good morning everyone,
First time posting in here, although I've been listening in for a few weeks now.
So this one has got me stumped, I am fairly new to PHP but I cannot seem
to Google through this one.
I cannot figure out how to 'exclude' PSComputerName and RunspaceId, which
is ultimately what I'm struggling with. Please see below for script snips
and explanations.
Also if there are any best practices you would recommend I'm open to it
being that I am fairly new and self-taught to PHP scripting.
<?php
###############
## Variables ##
###############
$TargetName = "Target1";
$login = "\$cred = New-Object System.Management.Automation.PSCredential
-String 'MyPassword' -AsPlainText -Force))";
$command = "Invoke-Command -computername 127.0.0.1 -credential \$cred
-scriptblock {& Get-IscsiServerTarget -TargetName " . $TargetName . " | % {
\$_.TargetIqn, \$_.Status}} -SessionOption (New-PSSessionOption
-SkipCACheck -SkipCNCheck -SkipRevocationCheck)";
$psCMD = "powershell -ExecutionPolicy Unrestricted -command \"$login; $command\" <NUL";
########################################
## Variable Checking (For Debug Mode) ##
########################################
#echo "\$psCMD = $psCMD";
################
## Run Script ##
################
exec($psCMD,$out);
############
## Output ##
############
echo ('<pre>');
print_r($out);
echo ('</pre>');
###################
## End Of Script ##
###################
echo "End Of Scene";
?>
Array
(
[0] => iqn.2013-04.com.widget:Target1
[1] =>
[2] => PSComputerName RunspaceId Value
[3] => -------------- ---------- -----
[4] => 127.0.0.1 52fb8b1b-8d8b-4eec-9419... NotConnected
[5] =>
[6] =>
)
End Of Scene
What I should see so I can then turn it into variables is what I run when
I run the command straight through the local Powershell command prompt
PS C:\Users\administrator.WIDGET.000> Get-IscsiServerTarget | % { $_.TargetIqn, $_.Status}
iqn.2013-04.com.widget:Target1
NotConnected
iqn.2013-04.com.widget:Target2
NotConnected
iqn.2013-04.com.widget:Target3
NotConnected
iqn.2013-04.com.widget:Target4
Connected
iqn.2013-04.com.widget:Target5
NotConnected
iqn.2013-04.com.widget:Target6
NotConnected
I cannot figure out how to 'exclude' PSComputerName and RunspaceId
Sorry in advance for the wordy explanation.
Cheers!
Alan
CONFIDENTIALITY NOTICE: This e-mail and the attachment(s) hereto (if any)
contain confidential information that is privileged and intended only for
the addressee(s) hereof. If you are not an intended recipient, you are
hereby notified that any disclosure, copying, distribution or use of this
e-mail and/or the accompanying attachment(s) is strictly prohibited. If you
have received this e-mail in error, please immediately notify the sender by
return e-mail.****
****
** **
Tedd Sperling
2013-07-19 17:42:11 UTC
Permalink
To all:

Sorry for top posting and sorry for sounding like the list-police, but you need to trim the excess from your post. Hitting reply without considering that other have to read through a bunch of old add-on email is not good.

So, please just trim your post to what is important.

Cheers,

tedd
Post by Serge Fonville
Thank you for your clarification.
Have you considered placing the whole powershell -command parameter in a
.ps1 script and executing that instead?
The benefit would be that it is easier to read and test accordingly.
HTH
Kind regards/met vriendelijke groet,
Serge Fonville
--- BIG SNIP ---
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Alan Loos
2013-07-19 17:53:12 UTC
Permalink
Serge,
I have, the script itself allows for easy transition into a .ps1, what I have done is removed the # from the debug section to get the full command that is then easily 'copy and paste'-able into a Powershell prompt and I get the same response in Powershell directly.
Also in response to the lastest post to the thread I will snip off some content here. (Sorry first time at this)

The ultimate goal is to pull the two variables so I can pass them forward via a PHP script to a MySQL Database.
(ie iqn.2013-04.com.widget:Target1 and NotConnected in this case)

I believe that the command in Powershell is trying to gather the $Status on both the Invoke-Command and Get-iSCSIServerTarget commands. Which is why I am getting a table in the Powershell Console and browser.

Per your request I have rewritten the scripts:
(Included inline)
TestGetServerTarget.php
________________________________
<?php


###############
## Variables ##
###############

$psCMD = "powershell.exe -ExecutionPolicy Unrestricted";
$psFILE = "C:\\Arc\\scripts\\TestGetTarget.ps1";
$runCMD = $psCMD." ".$psFILE;

########################################
## Variable Checking (For Debug Mode) ##
########################################

#echo "\$psCMD = $psCMD";


################
## Run Script ##
################

exec($runCMD, $out);


############
## Output ##
############

echo ('<pre>');
print_r($out);
echo ('</pre>');


###################
## End Of Script ##
###################

echo "End Of Scene";


?>
________________________________


TestGetTarget.ps1
________________________________
###############
## Variables ##
###############

$cred = New-Object System.Management.Automation.PSCredential -ArgumentList @('***@widget.com',(ConvertTo-SecureString -String 'MyPassword' -AsPlainText -Force))
$command = Invoke-Command -computername localhost -credential $cred -scriptblock {& Get-IscsiServerTarget -TargetName Target1 | % { $_.TargetIqn, $_.Status}} -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)


#########################
## Execute Scriptblock ##
#########################

$command
________________________________

Powershell output of TestGetTarget.ps1:
________________________________
PS C:\Arc\Scripts> .\TestGetTarget.ps1
iqn.2013-04.com.widget:Target1

PSComputerName RunspaceId Value
-------------- ---------- -----
localhost f3c5063a-85df-49a3-a5ed-7df04a930684 NotConnected
________________________________

PHP output webbrowser (Much in the same):
________________________________
Array
(
[0] => iqn.2013-04.com.widget:Target1
[1] =>
[2] => PSComputerName RunspaceId Value
[3] => -------------- ---------- -----
[4] => localhost f46c9f15-70b4-496c-a9d6... NotConnected
[5] =>
[6] =>
)

End Of Scene
________________________________


Alan Loos

CONFIDENTIALITY NOTICE: This e-mail and the attachment(s) hereto (if any) contain confidential information that is privileged and intended only for the addressee(s) hereof. If you are not an intended recipient, you are hereby notified that any disclosure, copying, distribution or use of this e-mail and/or the accompanying attachment(s) is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender by return e-mail.

From: Serge Fonville [mailto:***@gmail.com]
Sent: Friday, July 19, 2013 12:16 PM
To: Alan Loos
Cc: php-***@lists.php.net
Subject: Re: [PHP] PHP and Powershell

Thank you for your clarification.

Have you considered placing the whole powershell -command parameter in a .ps1 script and executing that instead?

The benefit would be that it is easier to read and test accordingly.

HTH

Kind regards/met vriendelijke groet,

Serge Fonville

http://www.sergefonville.nl

Convince Microsoft!
They need to add TRUNCATE PARTITION in SQL Server
https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table

--Big Snip--

Please make note of my new email address: ***@genco.com.
Serge Fonville
2013-07-22 09:08:53 UTC
Permalink
Hi,

Thanks for your change.

I understand what has happend, from MSDN forums
http://social.technet.microsoft.com/Forums/windowsserver/en-US/4b841530-9d8c-4d09-a77c-b89c6e0bafab/how-do-i-capture-data-from-invokecommand

http://technet.microsoft.com/en-us/library/jj612804.aspx describes that
Get-IscsiServerTarget returns an instance of
Microsoft.Iscsi.Target.Commands.IscsiServerTarget. De output also includes
the runspaceid and pscomputername. Invoke-Command also returns a runspaceid
(when executed with -computername).

You need to use Select-Object to output only the relevant fields instead.

HTH

Kind regards/met vriendelijke groet,

Serge Fonville

http://www.sergefonville.nl

Convince Microsoft!
They need to add TRUNCATE PARTITION in SQL Server
https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table
Post by Alan Loos
Serge,
I have, the script itself allows for easy transition into a .ps1, what I
have done is removed the # from the debug section to get the full command
that is then easily ‘copy and paste’-able into a Powershell prompt and I
get the same response in Powershell directly.
Also in response to the lastest post to the thread I will snip off some
content here. (Sorry first time at this)
The ultimate goal is to pull the two variables so I can pass them forward
via a PHP script to a MySQL Database.
(ie iqn.2013-04.com.widget:Target1 and NotConnected in this case)
I believe that the command in Powershell is trying to gather the $Status
on both the Invoke-Command and Get-iSCSIServerTarget commands. Which is why
I am getting a table in the Powershell Console and browser.
(Included inline)
TestGetServerTarget.php
------------------------------
<?php
###############
## Variables ##
###############
$psCMD = "powershell.exe -ExecutionPolicy Unrestricted";
$psFILE = "C:\\Arc\\scripts\\TestGetTarget.ps1";
$runCMD = $psCMD." ".$psFILE;
########################################
## Variable Checking (For Debug Mode) ##
########################################
#echo "\$psCMD = $psCMD";
################
## Run Script ##
################
exec($runCMD, $out);
############
## Output ##
############
echo ('<pre>');
print_r($out);
echo ('</pre>');
###################
## End Of Script ##
###################
echo "End Of Scene";
?>
------------------------------
TestGetTarget.ps1
------------------------------
###############
## Variables ##
###############
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList
@('***@widget.com',(ConvertTo-SecureString -String 'MyPassword'
-AsPlainText -Force))
$command = Invoke-Command -computername localhost -credential $cred
-scriptblock {& Get-IscsiServerTarget -TargetName Target1 | % {
$_.TargetIqn, $_.Status}} -SessionOption (New-PSSessionOption -SkipCACheck
-SkipCNCheck -SkipRevocationCheck)
#########################
## Execute Scriptblock ##
#########################
$command
------------------------------
------------------------------
PS C:\Arc\Scripts> .\TestGetTarget.ps1
iqn.2013-04.com.widget:Target1
PSComputerName RunspaceId
Value
--------------
---------- -----
localhost
f3c5063a-85df-49a3-a5ed-7df04a930684 NotConnected
------------------------------
------------------------------
Array
(
[0] => iqn.2013-04.com.widget:Target1
[1] =>
[2] => PSComputerName RunspaceId Value
[3] => -------------- ---------- -----
[4] => localhost f46c9f15-70b4-496c-a9d6... NotConnected
[5] =>
[6] =>
)
End Of Scene
------------------------------
*Alan Loos*
CONFIDENTIALITY NOTICE: This e-mail and the attachment(s) hereto (if any)
contain confidential information that is privileged and intended only for
the addressee(s) hereof. If you are not an intended recipient, you are
hereby notified that any disclosure, copying, distribution or use of this
e-mail and/or the accompanying attachment(s) is strictly prohibited. If you
have received this e-mail in error, please immediately notify the sender by
return e-mail.
*Sent:* Friday, July 19, 2013 12:16 PM
*To:* Alan Loos
*Subject:* Re: [PHP] PHP and Powershell
Thank you for your clarification.
Have you considered placing the whole powershell -command parameter in a
.ps1 script and executing that instead?
The benefit would be that it is easier to read and test accordingly.
HTH
Kind regards/met vriendelijke groet,
Serge Fonville
http://www.sergefonville.nl
Convince Microsoft!
They need to add TRUNCATE PARTITION in SQL Server
https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table
--Big Snip--
Alan Loos
2013-07-22 15:00:51 UTC
Permalink
Serge,

Won't lie, read this from my phone this morning and thought 'No it can't be that easy'.
Now that I'm at work in front of a computer I've updated the command to the following:

GetTarget.ps1
________________________________
###############
## Variables ##
###############

$cred = New-Object System.Management.Automation.PSCredential -ArgumentList @('***@widget.com',(ConvertTo-SecureString -String 'MyPassword' -AsPlainText -Force))
$command = Invoke-Command -computername localhost -credential $cred -scriptblock {& Get-IscsiServerTarget -TargetName Target1 | select-object TargetIqn,Status | Format-List} -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)


#########################
## Execute Scriptblock ##
#########################

$command
________________________________


And it returns:
________________________________
Array
(
[0] =>
[1] =>
[2] => TargetIqn : iqn.2013-04.com.widget:Target1
[3] => Status : NotConnected
[4] =>
[5] =>
[6] =>
)
0

End Of Scene
________________________________
In effect, you're a genius!

Thank you for all your help I should be able to tackle it from here!

Alan Loos

CONFIDENTIALITY NOTICE: This e-mail and the attachment(s) hereto (if any) contain confidential information that is privileged and intended only for the addressee(s) hereof. If you are not an intended recipient, you are hereby notified that any disclosure, copying, distribution or use of this e-mail and/or the accompanying attachment(s) is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender by return e-mail.

From: Serge Fonville [mailto:***@gmail.com]
Sent: Monday, July 22, 2013 4:09 AM
To: Alan Loos
Cc: php-***@lists.php.net
Subject: Re: [PHP] PHP and Powershell

Hi,

Thanks for your change.

I understand what has happend, from MSDN forums
http://social.technet.microsoft.com/Forums/windowsserver/en-US/4b841530-9d8c-4d09-a77c-b89c6e0bafab/how-do-i-capture-data-from-invokecommand

http://technet.microsoft.com/en-us/library/jj612804.aspx describes that
Get-IscsiServerTarget returns an instance of Microsoft.Iscsi.Target.Commands.IscsiServerTarget. De output also includes the runspaceid and pscomputername. Invoke-Command also returns a runspaceid (when executed with -computername).

You need to use Select-Object to output only the relevant fields instead.

HTH

Kind regards/met vriendelijke groet,

Serge Fonville

http://www.sergefonville.nl

Convince Microsoft!
They need to add TRUNCATE PARTITION in SQL Server
https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table

--Snip--
--Big Snip--


Please make note of my new email address: ***@genco.com.

Loading...