'Unauthorized operation' when executing Powershell.Invoke()












0















I am running the below code, as referenced from:



https://stackoverflow.com/questions/17067260/invoke-powershell-command-from-c-sharp-with-different-credential



I believe all necessary code is shown below but basically when GetPSResults is called, a PSCredential object is passed in. The issue arises when I try to execute ps.Invoke().



I am running this on localhost, running a Powershell script as a 'generic' user that has been created for use in this program (those credentials are set in the PSCredential object). This user has also been given 'admin' rights on my localhost. That user is trying to retrieve logs from a remote server. Given the error, I am assuming this is some type of permissions issue but I am not extremely familiar with that side of things.



protected override void OnStart(string args)
{
GetLogs newGetLogs = new GetLogs();
PSCredential credential = new PSCredential(@"MYDOMAINMYUSERID", newGetLogs.GetSecurePassword("TESTPASSWORD"));

Collection<PSObject> returnValues = GetPSResults(credential);

}
public static Collection<PSObject> GetPSResults(PSCredential credential, bool throwErrors = true)
{
Collection<PSObject> toReturn = new Collection<PSObject>();
WSManConnectionInfo connectionInfo = new WSManConnectionInfo();
connectionInfo.Credential = credential;

using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
runspace.Open();
using (PowerShell ps = PowerShell.Create())
{
ps.Runspace = runspace;
ps.AddCommand("get-eventlog");
ps.AddParameter("ComputerName", "MYSERVERNAME");
ps.AddParameter("LogName", "MYLOGNAME");
ps.AddParameter("EntryType", "Error");
ps.AddParameter("Newest", 20);

toReturn = ps.Invoke();
if (throwErrors)
{
if (ps.HadErrors)
{
throw ps.Streams.Error.ElementAt(0).Exception;
}
}
}
runspace.Close();
}

return toReturn;
}

public SecureString GetSecurePassword(string password)
{
var securePassword = new SecureString();
foreach (var c in password)
{
securePassword.AppendChar(c);
}

return securePassword;
}


I am getting this error:




System.Management.Automation.RemoteException: 'Attempted to perform an unauthorized operation.'




Within Visual Studio, I can drill down into the error to see the below image. I see that there are multiple mentions of Registry in these messages... is that a sign of some sort of permissions issue?



I have seen some mention of WinRM but I am not completely clear on the type of updates I would need to make... either in the code or on my user account(s).










share|improve this question























  • Is there a reason you are calling PowerShell from C# instead of using the .NET methods for reading the Windows Event Log? (like EventLog.GetEventLogs

    – Gabriel Luci
    Nov 16 '18 at 18:59











  • I assume this is in a Windows service. What credentials is the service running under?

    – Gabriel Luci
    Nov 16 '18 at 19:01











  • @GabrielLuci, great question. This particular app is planned to do some more involved operations, which will be easier with Powershell. Regarding your other question... Yes this is in a Windows service. If I understand you correctly, the service is running under localhost credentials.

    – tbcodes33
    Nov 16 '18 at 19:05













  • Do you mean that it is a local user account? If you run "services.msc" and look at the list of services, there is a "Log on as" column. What is there for this service?

    – Gabriel Luci
    Nov 16 '18 at 19:09













  • And I'd be curious to know what "more involved operations" are easier with PowerShell than through .NET. I have done PowerShell coding in C# before when I had no other choice (like with Microsoft Exchange and Office 365 administration) but it's awful. I have learned to avoid it at all costs.

    – Gabriel Luci
    Nov 16 '18 at 19:11


















0















I am running the below code, as referenced from:



https://stackoverflow.com/questions/17067260/invoke-powershell-command-from-c-sharp-with-different-credential



I believe all necessary code is shown below but basically when GetPSResults is called, a PSCredential object is passed in. The issue arises when I try to execute ps.Invoke().



I am running this on localhost, running a Powershell script as a 'generic' user that has been created for use in this program (those credentials are set in the PSCredential object). This user has also been given 'admin' rights on my localhost. That user is trying to retrieve logs from a remote server. Given the error, I am assuming this is some type of permissions issue but I am not extremely familiar with that side of things.



protected override void OnStart(string args)
{
GetLogs newGetLogs = new GetLogs();
PSCredential credential = new PSCredential(@"MYDOMAINMYUSERID", newGetLogs.GetSecurePassword("TESTPASSWORD"));

Collection<PSObject> returnValues = GetPSResults(credential);

}
public static Collection<PSObject> GetPSResults(PSCredential credential, bool throwErrors = true)
{
Collection<PSObject> toReturn = new Collection<PSObject>();
WSManConnectionInfo connectionInfo = new WSManConnectionInfo();
connectionInfo.Credential = credential;

using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
runspace.Open();
using (PowerShell ps = PowerShell.Create())
{
ps.Runspace = runspace;
ps.AddCommand("get-eventlog");
ps.AddParameter("ComputerName", "MYSERVERNAME");
ps.AddParameter("LogName", "MYLOGNAME");
ps.AddParameter("EntryType", "Error");
ps.AddParameter("Newest", 20);

toReturn = ps.Invoke();
if (throwErrors)
{
if (ps.HadErrors)
{
throw ps.Streams.Error.ElementAt(0).Exception;
}
}
}
runspace.Close();
}

return toReturn;
}

public SecureString GetSecurePassword(string password)
{
var securePassword = new SecureString();
foreach (var c in password)
{
securePassword.AppendChar(c);
}

return securePassword;
}


I am getting this error:




System.Management.Automation.RemoteException: 'Attempted to perform an unauthorized operation.'




Within Visual Studio, I can drill down into the error to see the below image. I see that there are multiple mentions of Registry in these messages... is that a sign of some sort of permissions issue?



I have seen some mention of WinRM but I am not completely clear on the type of updates I would need to make... either in the code or on my user account(s).










share|improve this question























  • Is there a reason you are calling PowerShell from C# instead of using the .NET methods for reading the Windows Event Log? (like EventLog.GetEventLogs

    – Gabriel Luci
    Nov 16 '18 at 18:59











  • I assume this is in a Windows service. What credentials is the service running under?

    – Gabriel Luci
    Nov 16 '18 at 19:01











  • @GabrielLuci, great question. This particular app is planned to do some more involved operations, which will be easier with Powershell. Regarding your other question... Yes this is in a Windows service. If I understand you correctly, the service is running under localhost credentials.

    – tbcodes33
    Nov 16 '18 at 19:05













  • Do you mean that it is a local user account? If you run "services.msc" and look at the list of services, there is a "Log on as" column. What is there for this service?

    – Gabriel Luci
    Nov 16 '18 at 19:09













  • And I'd be curious to know what "more involved operations" are easier with PowerShell than through .NET. I have done PowerShell coding in C# before when I had no other choice (like with Microsoft Exchange and Office 365 administration) but it's awful. I have learned to avoid it at all costs.

    – Gabriel Luci
    Nov 16 '18 at 19:11
















0












0








0








I am running the below code, as referenced from:



https://stackoverflow.com/questions/17067260/invoke-powershell-command-from-c-sharp-with-different-credential



I believe all necessary code is shown below but basically when GetPSResults is called, a PSCredential object is passed in. The issue arises when I try to execute ps.Invoke().



I am running this on localhost, running a Powershell script as a 'generic' user that has been created for use in this program (those credentials are set in the PSCredential object). This user has also been given 'admin' rights on my localhost. That user is trying to retrieve logs from a remote server. Given the error, I am assuming this is some type of permissions issue but I am not extremely familiar with that side of things.



protected override void OnStart(string args)
{
GetLogs newGetLogs = new GetLogs();
PSCredential credential = new PSCredential(@"MYDOMAINMYUSERID", newGetLogs.GetSecurePassword("TESTPASSWORD"));

Collection<PSObject> returnValues = GetPSResults(credential);

}
public static Collection<PSObject> GetPSResults(PSCredential credential, bool throwErrors = true)
{
Collection<PSObject> toReturn = new Collection<PSObject>();
WSManConnectionInfo connectionInfo = new WSManConnectionInfo();
connectionInfo.Credential = credential;

using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
runspace.Open();
using (PowerShell ps = PowerShell.Create())
{
ps.Runspace = runspace;
ps.AddCommand("get-eventlog");
ps.AddParameter("ComputerName", "MYSERVERNAME");
ps.AddParameter("LogName", "MYLOGNAME");
ps.AddParameter("EntryType", "Error");
ps.AddParameter("Newest", 20);

toReturn = ps.Invoke();
if (throwErrors)
{
if (ps.HadErrors)
{
throw ps.Streams.Error.ElementAt(0).Exception;
}
}
}
runspace.Close();
}

return toReturn;
}

public SecureString GetSecurePassword(string password)
{
var securePassword = new SecureString();
foreach (var c in password)
{
securePassword.AppendChar(c);
}

return securePassword;
}


I am getting this error:




System.Management.Automation.RemoteException: 'Attempted to perform an unauthorized operation.'




Within Visual Studio, I can drill down into the error to see the below image. I see that there are multiple mentions of Registry in these messages... is that a sign of some sort of permissions issue?



I have seen some mention of WinRM but I am not completely clear on the type of updates I would need to make... either in the code or on my user account(s).










share|improve this question














I am running the below code, as referenced from:



https://stackoverflow.com/questions/17067260/invoke-powershell-command-from-c-sharp-with-different-credential



I believe all necessary code is shown below but basically when GetPSResults is called, a PSCredential object is passed in. The issue arises when I try to execute ps.Invoke().



I am running this on localhost, running a Powershell script as a 'generic' user that has been created for use in this program (those credentials are set in the PSCredential object). This user has also been given 'admin' rights on my localhost. That user is trying to retrieve logs from a remote server. Given the error, I am assuming this is some type of permissions issue but I am not extremely familiar with that side of things.



protected override void OnStart(string args)
{
GetLogs newGetLogs = new GetLogs();
PSCredential credential = new PSCredential(@"MYDOMAINMYUSERID", newGetLogs.GetSecurePassword("TESTPASSWORD"));

Collection<PSObject> returnValues = GetPSResults(credential);

}
public static Collection<PSObject> GetPSResults(PSCredential credential, bool throwErrors = true)
{
Collection<PSObject> toReturn = new Collection<PSObject>();
WSManConnectionInfo connectionInfo = new WSManConnectionInfo();
connectionInfo.Credential = credential;

using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
runspace.Open();
using (PowerShell ps = PowerShell.Create())
{
ps.Runspace = runspace;
ps.AddCommand("get-eventlog");
ps.AddParameter("ComputerName", "MYSERVERNAME");
ps.AddParameter("LogName", "MYLOGNAME");
ps.AddParameter("EntryType", "Error");
ps.AddParameter("Newest", 20);

toReturn = ps.Invoke();
if (throwErrors)
{
if (ps.HadErrors)
{
throw ps.Streams.Error.ElementAt(0).Exception;
}
}
}
runspace.Close();
}

return toReturn;
}

public SecureString GetSecurePassword(string password)
{
var securePassword = new SecureString();
foreach (var c in password)
{
securePassword.AppendChar(c);
}

return securePassword;
}


I am getting this error:




System.Management.Automation.RemoteException: 'Attempted to perform an unauthorized operation.'




Within Visual Studio, I can drill down into the error to see the below image. I see that there are multiple mentions of Registry in these messages... is that a sign of some sort of permissions issue?



I have seen some mention of WinRM but I am not completely clear on the type of updates I would need to make... either in the code or on my user account(s).







c# powershell runspace






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 18:48









tbcodes33tbcodes33

33




33













  • Is there a reason you are calling PowerShell from C# instead of using the .NET methods for reading the Windows Event Log? (like EventLog.GetEventLogs

    – Gabriel Luci
    Nov 16 '18 at 18:59











  • I assume this is in a Windows service. What credentials is the service running under?

    – Gabriel Luci
    Nov 16 '18 at 19:01











  • @GabrielLuci, great question. This particular app is planned to do some more involved operations, which will be easier with Powershell. Regarding your other question... Yes this is in a Windows service. If I understand you correctly, the service is running under localhost credentials.

    – tbcodes33
    Nov 16 '18 at 19:05













  • Do you mean that it is a local user account? If you run "services.msc" and look at the list of services, there is a "Log on as" column. What is there for this service?

    – Gabriel Luci
    Nov 16 '18 at 19:09













  • And I'd be curious to know what "more involved operations" are easier with PowerShell than through .NET. I have done PowerShell coding in C# before when I had no other choice (like with Microsoft Exchange and Office 365 administration) but it's awful. I have learned to avoid it at all costs.

    – Gabriel Luci
    Nov 16 '18 at 19:11





















  • Is there a reason you are calling PowerShell from C# instead of using the .NET methods for reading the Windows Event Log? (like EventLog.GetEventLogs

    – Gabriel Luci
    Nov 16 '18 at 18:59











  • I assume this is in a Windows service. What credentials is the service running under?

    – Gabriel Luci
    Nov 16 '18 at 19:01











  • @GabrielLuci, great question. This particular app is planned to do some more involved operations, which will be easier with Powershell. Regarding your other question... Yes this is in a Windows service. If I understand you correctly, the service is running under localhost credentials.

    – tbcodes33
    Nov 16 '18 at 19:05













  • Do you mean that it is a local user account? If you run "services.msc" and look at the list of services, there is a "Log on as" column. What is there for this service?

    – Gabriel Luci
    Nov 16 '18 at 19:09













  • And I'd be curious to know what "more involved operations" are easier with PowerShell than through .NET. I have done PowerShell coding in C# before when I had no other choice (like with Microsoft Exchange and Office 365 administration) but it's awful. I have learned to avoid it at all costs.

    – Gabriel Luci
    Nov 16 '18 at 19:11



















Is there a reason you are calling PowerShell from C# instead of using the .NET methods for reading the Windows Event Log? (like EventLog.GetEventLogs

– Gabriel Luci
Nov 16 '18 at 18:59





Is there a reason you are calling PowerShell from C# instead of using the .NET methods for reading the Windows Event Log? (like EventLog.GetEventLogs

– Gabriel Luci
Nov 16 '18 at 18:59













I assume this is in a Windows service. What credentials is the service running under?

– Gabriel Luci
Nov 16 '18 at 19:01





I assume this is in a Windows service. What credentials is the service running under?

– Gabriel Luci
Nov 16 '18 at 19:01













@GabrielLuci, great question. This particular app is planned to do some more involved operations, which will be easier with Powershell. Regarding your other question... Yes this is in a Windows service. If I understand you correctly, the service is running under localhost credentials.

– tbcodes33
Nov 16 '18 at 19:05







@GabrielLuci, great question. This particular app is planned to do some more involved operations, which will be easier with Powershell. Regarding your other question... Yes this is in a Windows service. If I understand you correctly, the service is running under localhost credentials.

– tbcodes33
Nov 16 '18 at 19:05















Do you mean that it is a local user account? If you run "services.msc" and look at the list of services, there is a "Log on as" column. What is there for this service?

– Gabriel Luci
Nov 16 '18 at 19:09







Do you mean that it is a local user account? If you run "services.msc" and look at the list of services, there is a "Log on as" column. What is there for this service?

– Gabriel Luci
Nov 16 '18 at 19:09















And I'd be curious to know what "more involved operations" are easier with PowerShell than through .NET. I have done PowerShell coding in C# before when I had no other choice (like with Microsoft Exchange and Office 365 administration) but it's awful. I have learned to avoid it at all costs.

– Gabriel Luci
Nov 16 '18 at 19:11







And I'd be curious to know what "more involved operations" are easier with PowerShell than through .NET. I have done PowerShell coding in C# before when I had no other choice (like with Microsoft Exchange and Office 365 administration) but it's awful. I have learned to avoid it at all costs.

– Gabriel Luci
Nov 16 '18 at 19:11














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343739%2funauthorized-operation-when-executing-powershell-invoke%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343739%2funauthorized-operation-when-executing-powershell-invoke%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

鏡平學校

ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

Why https connections are so slow when debugging (stepping over) in Java?