Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8020865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:48:42+00:00 2026-06-04T21:48:42+00:00

I have a powershell script that works fine if I run it from an

  • 0

I have a powershell script that works fine if I run it from an admin powershell. However, if I call on the powershell script in a c program, ran as admin, using the system() function, some parts of it do not work. More specifically the copy unattend.xml, and sysprep.exe commands. I have posted the powershell script and the c program script. How can I get this to work?

POWERSHELL SCRIPT:

#Set working directory to scripts location.
$scriptpath = $MyInvocation.MyCommand.Path
$Switcheroo = Split-Path $scriptpath
Set-Location $Switcheroo
#Set error options
$Error.clear()
$ErrorActionPreference = “Inquire”

Write-Host "Running part 2."
#1
& "DISKPART" /s $Switcheroo\DskPrtRmv.txt
TIMEOUT /T 3

#2
if ($? -eq "True")
{
    Copy-Item $Switcheroo\unattend.xml $env:windir\System32\Sysprep
    TIMEOUT /T 3 

    #3
    if ($? -eq "True")
    {
        SCHTASKS /Delete /TN "Switcheroo" /f
        TIMEOUT /T 3
    }
    elseif ($? -ne "True")
    {
        Write-Host Failed to copy unattend.xml
        exit (22)
    }

        #4
        if ($? -eq "True")
        {
            rm log.txt
            TIMEOUT /T 3
        }
        elseif ($? -ne "True")
        {
            Write-Host Failed to delete the schedued task
            exit (32)
        }

            #5
            if ($? -eq "True")
            {
                & "$env:windir\System32\Sysprep\sysprep.exe" /generalize /oobe /shutdown /unattend:unattend.xml
            }
            elseif ($? -ne "True")
            {
                Write-Host Failed to remove the log.txt
                exit (42)
            }
                #6
                if ($? -ne "True")
                {
                    Write-Host Sysprep failed.
                    exit (52)
                }
}
elseif ($? -ne "True")
{
    Write-Host Failed to run DskPrtRmv
    exit (12)
}

C CODE:

/* 
* File:   main.c
* Author: Andrew
*
* Created on June 1, 2012, 2:39 PM
*/

#include <stdio.h>
#include <stdlib.h>

FILE *fp;

int main() 
{
    printf("Switcheroo in progress...\n");

    if ((fp=fopen("chk.bin", "rb")) == NULL)
    {
        //Run powershell script Part1.ps1 and set its return value to the int i variable.
        int i = system("powershell -executionpolicy unrestricted -file \"Part1.ps1\"");
        if (i == 0)
        {
            //Set up the log file that the computer will check upon reboot.
            char buffer[2] = {'0'};
            fp = fopen("chk.bin", "wb");
            fwrite (buffer , 1 , sizeof(buffer) , fp );
        }
        else if (i != 0)
        {
            //Print the error returned from powershell script Part1.ps1
            printf("Part1 Error: %d \n", i);
            system("PAUSE");
        }
    }
    else if (fp = fopen("chk.bin", "rb"))
    {
        //Run powershell script Part2.ps1 and set its return value to the int j variable.
        int j = system("powershell -executionpolicy unrestricted -file \"Part2.ps1\"");
        if (j == 0)
        {
            printf("Switcheroo has finished successfully.\n");
            remove("chk.bin");
        }
        else if (j != 0)
        {
            //Print the error returned from powershell script Part2.ps1
            printf("Part2 Error: %d \n", j);
            system("PAUSE");
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:48:44+00:00Added an answer on June 4, 2026 at 9:48 pm

    What is the architecture of operating system on the computer on which your are testing ?

    When you compile your C program do you target 32 bits or 64 bits exe ?

    Your trouble can be explained by the fact that your OS is 64 bits and your program 32 bits, so it executes a 32 bits version of PowerShell and this generates some trouble.

    If so, you can solve your problem spawning a 64 bits PowerShell from a 32 bits C exe using.

    c:\windows\sysnative\WindowsPowerShell\v1.0\powershell.exe
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a powershell script that modifies transport rules. It works perfectly from powershell
I have a complex Powershell script that gets run as part of a SQL
I have developed a Powershell script that needs to be run automatically daily. This
I have a SQL 2008 job that does database backups using a Powershell script.
I have a Powershell script that uses System.Net.HttpWebRequest to communicate with a remote host.
I have a PowerShell script that uses du.exe ( Disk Usage originally from Sysinternals)
I have a Powershell script that is going to be run through an automation
I have a Batch file which will call a Powershell Script : BATCH FILE
I have a fairly simple Psake build script (default.ps1) that calls Invoke-Psake from within
I have a Powershell script that reads values off of the pipeline: PARAM (

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.