C# Powershell code not executing on Windows 7How do I calculate someone's age in C#?What is the difference between String and string in C#?Hidden Features of C#?Cast int to enum in C#How do I enumerate an enum in C#?What are the correct version numbers for C#?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?Determine installed PowerShell versionPowerShell says “execution of scripts is disabled on this system.”How do you comment out code in PowerShell?
/api/sitecore is not working in CD server
What is the highest level of accuracy in motion control a Victorian society could achieve?
How can a ban from entering the US be lifted?
What is the difference between an "empty interior" and a "hole" in topology?
Is this car delivery via Ebay Motors on Craigslist a scam?
Can you take the Dodge action while prone?
Boss furious on bad appraisal
What is the shape of the upper boundary of water hitting a screen?
Does the force of friction helps us to accelerate while running?
Was I wrongfully denied boarding for having a Schengen visa issued from the second country on my itinerary?
Is it possible to spoof an IP address to an exact number?
Is this standard Japanese employment negotiations, or am I missing something?
What can a novel do that film and TV cannot?
Is there a standard definition of the "stall" phenomena?
Is conquering your neighbors to fight a greater enemy a valid strategy?
How can I effectively map a multi-level dungeon?
How to play a D major chord lower than the open E major chord on guitar?
What is it called when the tritone is added to a minor scale?
What's the difference between a type and a kind?
Do Goblin tokens count as Goblins?
What's the big deal about the Nazgûl losing their horses?
How can select a specific triangle in my Delaunay mesh?
What are some bad ways to subvert tropes?
Is there a way to change the aspect ratio of a DNG file?
C# Powershell code not executing on Windows 7
How do I calculate someone's age in C#?What is the difference between String and string in C#?Hidden Features of C#?Cast int to enum in C#How do I enumerate an enum in C#?What are the correct version numbers for C#?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?Determine installed PowerShell versionPowerShell says “execution of scripts is disabled on this system.”How do you comment out code in PowerShell?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Powershell code executed through my C# application is executing perfectly on Windows 10, but won't even run on Windows 7. Neither does it get beyond Process.WaitForExit()
I've tried using Powershell (System.Management.Automation) itself inside the code.
I've also set 'set-executionpolicy remotesigned' and it runs using a powershell script (.ps1), but not when using it in my C# code as shown below.
Process p = new Process();
p.StartInfo.FileName = "powershell";
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.Start();
using (StreamWriter sw = p.StandardInput)
if (sw.BaseStream.CanWrite)
string pdfName = fileName.Substring(0, fileName.Length - new FileInfo(fileName).Extension.Length) + ".pdf";
sw.WriteLine("$Exl = New-Object -ComObject Excel.Application");
sw.WriteLine("$Doc = $Exl.Workbooks.Open("" + fileName + "")");
sw.WriteLine("$Doc.ExportAsFixedFormat([Microsoft.Office.Interop.Excel.XlFixedFormatType]::xlTypePDF, "" + pdfName +"")");
sw.WriteLine("$Doc.Close($False)");
sw.WriteLine("[gc]::Collect()");
sw.WriteLine("[gc]::WaitForPendingFinalizers()");
sw.WriteLine("$Exl.Quit()");
sw.WriteLine("[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Exl)");
sw.WriteLine("Remove-Variable Exl");
p.WaitForExit();
On Windows 10, the script successfully converts an Excel file to pdf, but on Windows 7 - the script doesn't even open Excel to convert the file.
c# powershell
|
show 5 more comments
Powershell code executed through my C# application is executing perfectly on Windows 10, but won't even run on Windows 7. Neither does it get beyond Process.WaitForExit()
I've tried using Powershell (System.Management.Automation) itself inside the code.
I've also set 'set-executionpolicy remotesigned' and it runs using a powershell script (.ps1), but not when using it in my C# code as shown below.
Process p = new Process();
p.StartInfo.FileName = "powershell";
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.Start();
using (StreamWriter sw = p.StandardInput)
if (sw.BaseStream.CanWrite)
string pdfName = fileName.Substring(0, fileName.Length - new FileInfo(fileName).Extension.Length) + ".pdf";
sw.WriteLine("$Exl = New-Object -ComObject Excel.Application");
sw.WriteLine("$Doc = $Exl.Workbooks.Open("" + fileName + "")");
sw.WriteLine("$Doc.ExportAsFixedFormat([Microsoft.Office.Interop.Excel.XlFixedFormatType]::xlTypePDF, "" + pdfName +"")");
sw.WriteLine("$Doc.Close($False)");
sw.WriteLine("[gc]::Collect()");
sw.WriteLine("[gc]::WaitForPendingFinalizers()");
sw.WriteLine("$Exl.Quit()");
sw.WriteLine("[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Exl)");
sw.WriteLine("Remove-Variable Exl");
p.WaitForExit();
On Windows 10, the script successfully converts an Excel file to pdf, but on Windows 7 - the script doesn't even open Excel to convert the file.
c# powershell
4
I'm curious why you're invoking powershell to do that instead of just using Excel interop directly.
– itsme86
Mar 25 at 19:51
1
What version ofSystem.Management.Automation
have you linked to in the project? By default Windows 7 only has PowerShell v2; if you referenced a later assembly that might be why it never runs.
– briantist
Mar 25 at 19:53
maybe can catch exception to see the issue?
– urlreader
Mar 25 at 20:09
@itsme86 I'm asking myself the same question. It works, thank you. Only thing is, i'm still wondering why the powershell script is executing fine on Win10 but not on Win7?
– Ian N
Mar 25 at 20:38
I'm glad it works. Not sure about the Win7 powershell problem. Is @briantist onto something maybe?
– itsme86
Mar 25 at 20:41
|
show 5 more comments
Powershell code executed through my C# application is executing perfectly on Windows 10, but won't even run on Windows 7. Neither does it get beyond Process.WaitForExit()
I've tried using Powershell (System.Management.Automation) itself inside the code.
I've also set 'set-executionpolicy remotesigned' and it runs using a powershell script (.ps1), but not when using it in my C# code as shown below.
Process p = new Process();
p.StartInfo.FileName = "powershell";
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.Start();
using (StreamWriter sw = p.StandardInput)
if (sw.BaseStream.CanWrite)
string pdfName = fileName.Substring(0, fileName.Length - new FileInfo(fileName).Extension.Length) + ".pdf";
sw.WriteLine("$Exl = New-Object -ComObject Excel.Application");
sw.WriteLine("$Doc = $Exl.Workbooks.Open("" + fileName + "")");
sw.WriteLine("$Doc.ExportAsFixedFormat([Microsoft.Office.Interop.Excel.XlFixedFormatType]::xlTypePDF, "" + pdfName +"")");
sw.WriteLine("$Doc.Close($False)");
sw.WriteLine("[gc]::Collect()");
sw.WriteLine("[gc]::WaitForPendingFinalizers()");
sw.WriteLine("$Exl.Quit()");
sw.WriteLine("[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Exl)");
sw.WriteLine("Remove-Variable Exl");
p.WaitForExit();
On Windows 10, the script successfully converts an Excel file to pdf, but on Windows 7 - the script doesn't even open Excel to convert the file.
c# powershell
Powershell code executed through my C# application is executing perfectly on Windows 10, but won't even run on Windows 7. Neither does it get beyond Process.WaitForExit()
I've tried using Powershell (System.Management.Automation) itself inside the code.
I've also set 'set-executionpolicy remotesigned' and it runs using a powershell script (.ps1), but not when using it in my C# code as shown below.
Process p = new Process();
p.StartInfo.FileName = "powershell";
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.Start();
using (StreamWriter sw = p.StandardInput)
if (sw.BaseStream.CanWrite)
string pdfName = fileName.Substring(0, fileName.Length - new FileInfo(fileName).Extension.Length) + ".pdf";
sw.WriteLine("$Exl = New-Object -ComObject Excel.Application");
sw.WriteLine("$Doc = $Exl.Workbooks.Open("" + fileName + "")");
sw.WriteLine("$Doc.ExportAsFixedFormat([Microsoft.Office.Interop.Excel.XlFixedFormatType]::xlTypePDF, "" + pdfName +"")");
sw.WriteLine("$Doc.Close($False)");
sw.WriteLine("[gc]::Collect()");
sw.WriteLine("[gc]::WaitForPendingFinalizers()");
sw.WriteLine("$Exl.Quit()");
sw.WriteLine("[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Exl)");
sw.WriteLine("Remove-Variable Exl");
p.WaitForExit();
On Windows 10, the script successfully converts an Excel file to pdf, but on Windows 7 - the script doesn't even open Excel to convert the file.
c# powershell
c# powershell
asked Mar 25 at 19:48
Ian NIan N
1
1
4
I'm curious why you're invoking powershell to do that instead of just using Excel interop directly.
– itsme86
Mar 25 at 19:51
1
What version ofSystem.Management.Automation
have you linked to in the project? By default Windows 7 only has PowerShell v2; if you referenced a later assembly that might be why it never runs.
– briantist
Mar 25 at 19:53
maybe can catch exception to see the issue?
– urlreader
Mar 25 at 20:09
@itsme86 I'm asking myself the same question. It works, thank you. Only thing is, i'm still wondering why the powershell script is executing fine on Win10 but not on Win7?
– Ian N
Mar 25 at 20:38
I'm glad it works. Not sure about the Win7 powershell problem. Is @briantist onto something maybe?
– itsme86
Mar 25 at 20:41
|
show 5 more comments
4
I'm curious why you're invoking powershell to do that instead of just using Excel interop directly.
– itsme86
Mar 25 at 19:51
1
What version ofSystem.Management.Automation
have you linked to in the project? By default Windows 7 only has PowerShell v2; if you referenced a later assembly that might be why it never runs.
– briantist
Mar 25 at 19:53
maybe can catch exception to see the issue?
– urlreader
Mar 25 at 20:09
@itsme86 I'm asking myself the same question. It works, thank you. Only thing is, i'm still wondering why the powershell script is executing fine on Win10 but not on Win7?
– Ian N
Mar 25 at 20:38
I'm glad it works. Not sure about the Win7 powershell problem. Is @briantist onto something maybe?
– itsme86
Mar 25 at 20:41
4
4
I'm curious why you're invoking powershell to do that instead of just using Excel interop directly.
– itsme86
Mar 25 at 19:51
I'm curious why you're invoking powershell to do that instead of just using Excel interop directly.
– itsme86
Mar 25 at 19:51
1
1
What version of
System.Management.Automation
have you linked to in the project? By default Windows 7 only has PowerShell v2; if you referenced a later assembly that might be why it never runs.– briantist
Mar 25 at 19:53
What version of
System.Management.Automation
have you linked to in the project? By default Windows 7 only has PowerShell v2; if you referenced a later assembly that might be why it never runs.– briantist
Mar 25 at 19:53
maybe can catch exception to see the issue?
– urlreader
Mar 25 at 20:09
maybe can catch exception to see the issue?
– urlreader
Mar 25 at 20:09
@itsme86 I'm asking myself the same question. It works, thank you. Only thing is, i'm still wondering why the powershell script is executing fine on Win10 but not on Win7?
– Ian N
Mar 25 at 20:38
@itsme86 I'm asking myself the same question. It works, thank you. Only thing is, i'm still wondering why the powershell script is executing fine on Win10 but not on Win7?
– Ian N
Mar 25 at 20:38
I'm glad it works. Not sure about the Win7 powershell problem. Is @briantist onto something maybe?
– itsme86
Mar 25 at 20:41
I'm glad it works. Not sure about the Win7 powershell problem. Is @briantist onto something maybe?
– itsme86
Mar 25 at 20:41
|
show 5 more comments
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55345375%2fc-sharp-powershell-code-not-executing-on-windows-7%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
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55345375%2fc-sharp-powershell-code-not-executing-on-windows-7%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
4
I'm curious why you're invoking powershell to do that instead of just using Excel interop directly.
– itsme86
Mar 25 at 19:51
1
What version of
System.Management.Automation
have you linked to in the project? By default Windows 7 only has PowerShell v2; if you referenced a later assembly that might be why it never runs.– briantist
Mar 25 at 19:53
maybe can catch exception to see the issue?
– urlreader
Mar 25 at 20:09
@itsme86 I'm asking myself the same question. It works, thank you. Only thing is, i'm still wondering why the powershell script is executing fine on Win10 but not on Win7?
– Ian N
Mar 25 at 20:38
I'm glad it works. Not sure about the Win7 powershell problem. Is @briantist onto something maybe?
– itsme86
Mar 25 at 20:41