4

管理者のPowerShellから実行すると、正常に機能するPowerShellスクリプトがあります。ただし、system()関数を使用して管理者として実行されたacプログラムでPowerShellスクリプトを呼び出すと、その一部が機能しません。具体的には、una​​ttend.xmlコマンドとsysprep.exeコマンドをコピーします。PowerShellスクリプトとcプログラムスクリプトを投稿しました。どうすればこれを機能させることができますか?

PowerShellスクリプト:

#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コード:

/* 
* 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");
        }
    }
}
4

1 に答える 1

4

テストしているコンピューターのオペレーティングシステムのアーキテクチャは何ですか?

プログラムをコンパイルするとき、C32ビットまたは64ビットのexeをターゲットにしますか?

あなたの問題はあなたのOSが64ビットであなたのプログラムが32ビットであるという事実によって説明することができます、それでそれはPowerShellの32ビットバージョンを実行します、そしてこれはいくつかの問題を引き起こします。

その場合は、を使用して32ビットのCexeから64ビットのPowerShellを生成する際の問題を解決できます。

c:\windows\sysnative\WindowsPowerShell\v1.0\powershell.exe
于 2012-06-03T07:02:36.937 に答える