4

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");
        }
    }
}
3
  • 1
    What does "does not work" mean? Commented Jun 3, 2012 at 6:23
  • When I run the powershell script in my c program copy unattend.xml does not return any errors, yet I have no copy in sysprep folder. No errors, just doesn't copy. If I run the powershell script from my c program and use it in my cmd prompt, it works. Commented Jun 3, 2012 at 6:51
  • If I change the output dir to my desktop, the program works fine. It is only an issue when I try to access \Windows\System32\Sysprep Commented Jun 3, 2012 at 7:02

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

4 Comments

I am using mingw to compile in netbeans. My os is 64 bits, and I compile in 32. Is you above example the location of the 64 bit powershell?
I do not have \Windows\sysnative folder. only system32 and sysWOW64: SysWOW64 does have the shell, I will try it.
Don't look for sysnative. It's virtual and used by the OS. If your OS is 64 bits and your exe 32 bits, just try it.
Worked perfectly thank you! If I need to run it on a 32 bit os I would need to use my original command correct?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.