Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - WCF calling the powershell

Reply
 
Old 08-03-2009   #1 (permalink)
Steffen Mangold


 
 

WCF calling the powershell

Hi, i have a question.

I developt a PSCommandLet and install it ob a PC. When i call it from the PC
all works fine.

Now i write a WCF Service with some methods and they methods call some of my
powershell CmdLets. When i now run a client on a other PC and call the WCF
Service methods i always get an exception that my powershell-snap-in is not
installed on the PC. Why it works local but not if an client call the WCF
Service and the service call the PS???

Here my code:

RunspaceConfiguration config = RunspaceConfiguration.Create();


Runspace runspace = RunspaceFactory.CreateRunspace();

runspace.Open();

PSSnapInException ex = new PSSnapInException();

runspace.RunspaceConfiguration.AddPSSnapIn("SunvoltEnergyStudioPowerShell",
out ex);

Collection<Project> projects = new Collection<Project>();
Pipeline getProjectsPipeline = runspace.CreatePipeline();
getProjectsPipeline.Commands.Add(new Command("Get-Projects"));
Collection<PSObject> results = getProjectsPipeline.Invoke();



My System SpecsSystem Spec
Old 08-03-2009   #2 (permalink)
Steffen Mangold


 
 

RE: WCF calling the powershell

Hi Bob,
sorry if i not clear.

PC A -> CmdLet -> WCF
PC B -> Client App.

PC B call the WCF Service on PC A
The PC A called the CmDLet and returnd a Result

The exception is thrown on AddPSSnapIn call.
On PC A i tested the CmdLet manuall, it is installed and work fine.

But if PC B called the WCF and the WCF methode try to add the Snapin it fail.

"Bob Landau" wrote:
Quote:

> At least for me your description below isn't clear.
>
> You have to machines A and B.
>
> Can you use your snapin on both machines directly?
> Can you use your snapin on any machine if its called with the WCF service?
>
> On whatever machine which the snapin is not working, have you first
> registered the snapin?
>
> What line of code is throwing the exception?
>
> "Steffen Mangold" wrote:
>
Quote:

> > Hi, i have a question.
> >
> > I developt a PSCommandLet and install it ob a PC. When i call it from the PC
> > all works fine.
> >
> > Now i write a WCF Service with some methods and they methods call some of my
> > powershell CmdLets. When i now run a client on a other PC and call the WCF
> > Service methods i always get an exception that my powershell-snap-in is not
> > installed on the PC. Why it works local but not if an client call the WCF
> > Service and the service call the PS???
> >
> > Here my code:
> >
> > RunspaceConfiguration config = RunspaceConfiguration.Create();
> >
> >
> > Runspace runspace = RunspaceFactory.CreateRunspace();
> >
> > runspace.Open();
> >
> > PSSnapInException ex = new PSSnapInException();
> >
> > runspace.RunspaceConfiguration.AddPSSnapIn("SunvoltEnergyStudioPowerShell",
> > out ex);
> >
> > Collection<Project> projects = new Collection<Project>();
> > Pipeline getProjectsPipeline = runspace.CreatePipeline();
> > getProjectsPipeline.Commands.Add(new Command("Get-Projects"));
> > Collection<PSObject> results = getProjectsPipeline.Invoke();
> >
> >
My System SpecsSystem Spec
Old 08-03-2009   #3 (permalink)
Bob Landau


 
 

RE: WCF calling the powershell

Steffen,

I've never created a snapin, about the time I started getting interested in
Powershell v2 was released as a CTP and modules at least to my understanding
have replaced snapin's any case thats the route I went.

Perhaps someone that has a better understanding of snapins can help.

Here is what I understand

PC B : Client-App => PC A: WFC service (Add's and uses your snapin)

The WFC service is most likely running under the credentials of
"NetworkService" and I wouldn't recomend changing it _except_ possible to
verify if that's the problem.

This account is limited on purpose I suspect the reason for the exception
(BTW what is the exception being thrown?) is because AddSnapin requires more
priviledges than this account is capabile of.

If this does turn out to be the issue you will need to add the snapin by
impersonating an account that has these priviledges and then revert back to
the process account since you will want this service running with the mimimal
priviledges that are required.

"Steffen Mangold" wrote:
Quote:

> Hi Bob,
> sorry if i not clear.
>
> PC A -> CmdLet -> WCF
> PC B -> Client App.
>
> PC B call the WCF Service on PC A
> The PC A called the CmDLet and returnd a Result
>
> The exception is thrown on AddPSSnapIn call.
> On PC A i tested the CmdLet manuall, it is installed and work fine.
>
> But if PC B called the WCF and the WCF methode try to add the Snapin it fail.
>
> "Bob Landau" wrote:
>
Quote:

> > At least for me your description below isn't clear.
> >
> > You have to machines A and B.
> >
> > Can you use your snapin on both machines directly?
> > Can you use your snapin on any machine if its called with the WCF service?
> >
> > On whatever machine which the snapin is not working, have you first
> > registered the snapin?
> >
> > What line of code is throwing the exception?
> >
> > "Steffen Mangold" wrote:
> >
Quote:

> > > Hi, i have a question.
> > >
> > > I developt a PSCommandLet and install it ob a PC. When i call it from the PC
> > > all works fine.
> > >
> > > Now i write a WCF Service with some methods and they methods call some of my
> > > powershell CmdLets. When i now run a client on a other PC and call the WCF
> > > Service methods i always get an exception that my powershell-snap-in is not
> > > installed on the PC. Why it works local but not if an client call the WCF
> > > Service and the service call the PS???
> > >
> > > Here my code:
> > >
> > > RunspaceConfiguration config = RunspaceConfiguration.Create();
> > >
> > >
> > > Runspace runspace = RunspaceFactory.CreateRunspace();
> > >
> > > runspace.Open();
> > >
> > > PSSnapInException ex = new PSSnapInException();
> > >
> > > runspace.RunspaceConfiguration.AddPSSnapIn("SunvoltEnergyStudioPowerShell",
> > > out ex);
> > >
> > > Collection<Project> projects = new Collection<Project>();
> > > Pipeline getProjectsPipeline = runspace.CreatePipeline();
> > > getProjectsPipeline.Commands.Add(new Command("Get-Projects"));
> > > Collection<PSObject> results = getProjectsPipeline.Invoke();
> > >
> > >
My System SpecsSystem Spec
Old 08-03-2009   #4 (permalink)
Marco Shaw [MVP]


 
 

Re: WCF calling the powershell

> I've never created a snapin, about the time I started getting interested
Quote:

> in
> Powershell v2 was released as a CTP and modules at least to my
> understanding
> have replaced snapin's any case thats the route I went.
>
> Perhaps someone that has a better understanding of snapins can help.
Yes, you've got it right... v1 snapins => v2 modules. Now that being said,
v2 modules provide loads of extra features like they can be regular scripts
and don't need to be compiled like snapins did (who had to be written in
..NET).

Marco

My System SpecsSystem Spec
Old 08-04-2009   #5 (permalink)
Steffen Mangold


 
 

Re: WCF calling the powershell

Yes, but this makes no differents to my problem.

Can nobody help me with that?

"Marco Shaw [MVP]" wrote:
Quote:
Quote:

> > I've never created a snapin, about the time I started getting interested
> > in
> > Powershell v2 was released as a CTP and modules at least to my
> > understanding
> > have replaced snapin's any case thats the route I went.
> >
> > Perhaps someone that has a better understanding of snapins can help.
>
> Yes, you've got it right... v1 snapins => v2 modules. Now that being said,
> v2 modules provide loads of extra features like they can be regular scripts
> and don't need to be compiled like snapins did (who had to be written in
> ..NET).
>
> Marco
>
>
My System SpecsSystem Spec
Old 08-04-2009   #6 (permalink)
Steffen Mangold


 
 

Re: WCF calling the powershell

I play i bit with this case and found that it is not possible to a a snapin
form a service. Because always exception "not installed on machine". Seems to
be a privileg problem. But do i need admin rights to add a snapin???
My System SpecsSystem Spec
Old 08-04-2009   #7 (permalink)
Joel Bennett [MVP]


 
 

Re: WCF calling the powershell

No. You need admin rights to INSTALL a snapin originally (because you
have to 'register' it), but not to add it to a powershell session.

This, incidentally, is why loading them as "modules" in v2 is cool: they
don't have to be 'registered' first, so you don't have to be admin to
install a module (which can be just a snapin).


You're sure the snapin has been installed and can be loaded right?
You're sure that's the full name of the snapin?
Have you tried printing out the errors:

runspace.RunspaceConfiguration.AddPSSnapIn(
"SunvoltEnergyStudioPowerShell", out ex);

if (ex!= null)
{
System.Console.Write(ex.Message);
return;
}

I'm afraid I don't have many ideas, if the snapin is registered, it
should show up and be addable for all users, as far as I know.

--
Joel


Steffen Mangold wrote:
Quote:

> I play i bit with this case and found that it is not possible to a a snapin
> form a service. Because always exception "not installed on machine". Seems to
> be a privileg problem. But do i need admin rights to add a snapin???
My System SpecsSystem Spec
Old 08-04-2009   #8 (permalink)
Bob Landau


 
 

Re: WCF calling the powershell

The credentials used for services such as NetworkService is truely a
different animal. They have far less credentials than the normal user for
security reasons.

I had mentioned it briefly above, based on the difficulty your having I
believe you'll need to impersonate a user account prior to calling AddSnapin.

Steffen if you search the web for Powershell and ASP.NET I'm think you an
answer to your question.

Here is one possible
http://support.microsoft.com/default.aspx/kb/306158

"Joel Bennett [MVP]" wrote:
Quote:

> No. You need admin rights to INSTALL a snapin originally (because you
> have to 'register' it), but not to add it to a powershell session.
>
> This, incidentally, is why loading them as "modules" in v2 is cool: they
> don't have to be 'registered' first, so you don't have to be admin to
> install a module (which can be just a snapin).
>
>
> You're sure the snapin has been installed and can be loaded right?
> You're sure that's the full name of the snapin?
> Have you tried printing out the errors:
>
> runspace.RunspaceConfiguration.AddPSSnapIn(
> "SunvoltEnergyStudioPowerShell", out ex);
>
> if (ex!= null)
> {
> System.Console.Write(ex.Message);
> return;
> }
>
> I'm afraid I don't have many ideas, if the snapin is registered, it
> should show up and be addable for all users, as far as I know.
>
> --
> Joel
>
>
> Steffen Mangold wrote:
Quote:

> > I play i bit with this case and found that it is not possible to a a snapin
> > form a service. Because always exception "not installed on machine". Seems to
> > be a privileg problem. But do i need admin rights to add a snapin???
>
My System SpecsSystem Spec
Old 08-04-2009   #9 (permalink)
Steffen Mangold


 
 

Re: WCF calling the powershell

I run the Server on a x64 OS. And i find a other bug i think that can have
the same reason. The WCF Service is x32 compiled, normally (such a normal
service) if a app. is complied in x32, if it access the Regedit it goes to
the WOW64 nodes. All my services works so, but if i call a WCF method from
remote and go to regedit if goes to the x64 nodes. That should not happen
normally!! So it seems there is some problem in the running WCF service (the
WCF service is hosted by a normal x32 WinService)

"Bob Landau" wrote:
Quote:

> The credentials used for services such as NetworkService is truely a
> different animal. They have far less credentials than the normal user for
> security reasons.
>
> I had mentioned it briefly above, based on the difficulty your having I
> believe you'll need to impersonate a user account prior to calling AddSnapin.
>
> Steffen if you search the web for Powershell and ASP.NET I'm think you an
> answer to your question.
>
> Here is one possible
> http://support.microsoft.com/default.aspx/kb/306158
>
> "Joel Bennett [MVP]" wrote:
>
Quote:

> > No. You need admin rights to INSTALL a snapin originally (because you
> > have to 'register' it), but not to add it to a powershell session.
> >
> > This, incidentally, is why loading them as "modules" in v2 is cool: they
> > don't have to be 'registered' first, so you don't have to be admin to
> > install a module (which can be just a snapin).
> >
> >
> > You're sure the snapin has been installed and can be loaded right?
> > You're sure that's the full name of the snapin?
> > Have you tried printing out the errors:
> >
> > runspace.RunspaceConfiguration.AddPSSnapIn(
> > "SunvoltEnergyStudioPowerShell", out ex);
> >
> > if (ex!= null)
> > {
> > System.Console.Write(ex.Message);
> > return;
> > }
> >
> > I'm afraid I don't have many ideas, if the snapin is registered, it
> > should show up and be addable for all users, as far as I know.
> >
> > --
> > Joel
> >
> >
> > Steffen Mangold wrote:
Quote:

> > > I play i bit with this case and found that it is not possible to a a snapin
> > > form a service. Because always exception "not installed on machine". Seems to
> > > be a privileg problem. But do i need admin rights to add a snapin???
> >
My System SpecsSystem Spec
Old 08-05-2009   #10 (permalink)
Bob Landau


 
 

Re: WCF calling the powershell

Seems to me like your too close to the solution. One of the two issues are
likely what is causing your problem.

1) Registered the snapin (and assume it will work as both a x64 and x32 app)
It will not. You must register the application using both

2) A 32 bit DLL won't fit in a x64 host and visa-versa. Why are you assuming
that this DLL is 32 bit? This means you DLL must be 64 bit based on your
detective work.

This is managed code correct? By default when its GIT'ed the runtime will
pick the native environment over the emulated which for you is 64bit.

So you can either force the runtime to GIT a 32 bit DLL (there's a program
option for this).
btw I ran into the same problem once when I tried to load a 32 bit COM DLL
in managed code. your not the only one that has been bitten by this

Or you can register this snapin for both 32 bit and 64 bit. Prior to trying
anything else verify that from both the 32 and 64 bit Powershell shell you
can call Add-PSSnapin.

If even after this you are still having problems the issue may still be want
I mentioned originally.

"Steffen Mangold" wrote:
Quote:

> I run the Server on a x64 OS. And i find a other bug i think that can have
> the same reason. The WCF Service is x32 compiled, normally (such a normal
> service) if a app. is complied in x32, if it access the Regedit it goes to
> the WOW64 nodes. All my services works so, but if i call a WCF method from
> remote and go to regedit if goes to the x64 nodes. That should not happen
> normally!! So it seems there is some problem in the running WCF service (the
> WCF service is hosted by a normal x32 WinService)
>
> "Bob Landau" wrote:
>
Quote:

> > The credentials used for services such as NetworkService is truely a
> > different animal. They have far less credentials than the normal user for
> > security reasons.
> >
> > I had mentioned it briefly above, based on the difficulty your having I
> > believe you'll need to impersonate a user account prior to calling AddSnapin.
> >
> > Steffen if you search the web for Powershell and ASP.NET I'm think you an
> > answer to your question.
> >
> > Here is one possible
> > http://support.microsoft.com/default.aspx/kb/306158
> >
> > "Joel Bennett [MVP]" wrote:
> >
Quote:

> > > No. You need admin rights to INSTALL a snapin originally (because you
> > > have to 'register' it), but not to add it to a powershell session.
> > >
> > > This, incidentally, is why loading them as "modules" in v2 is cool: they
> > > don't have to be 'registered' first, so you don't have to be admin to
> > > install a module (which can be just a snapin).
> > >
> > >
> > > You're sure the snapin has been installed and can be loaded right?
> > > You're sure that's the full name of the snapin?
> > > Have you tried printing out the errors:
> > >
> > > runspace.RunspaceConfiguration.AddPSSnapIn(
> > > "SunvoltEnergyStudioPowerShell", out ex);
> > >
> > > if (ex!= null)
> > > {
> > > System.Console.Write(ex.Message);
> > > return;
> > > }
> > >
> > > I'm afraid I don't have many ideas, if the snapin is registered, it
> > > should show up and be addable for all users, as far as I know.
> > >
> > > --
> > > Joel
> > >
> > >
> > > Steffen Mangold wrote:
> > > > I play i bit with this case and found that it is not possible to a a snapin
> > > > form a service. Because always exception "not installed on machine". Seems to
> > > > be a privileg problem. But do i need admin rights to add a snapin???
> > >
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
calling gnu utilities in a PowerShell script PowerShell
calling Sharepoint services using powershell PowerShell
Calling applications from powershell scripts PowerShell
Calling a stored procedure from powershell PowerShell
calling dtexec.exe from powershell PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46