-1

I have a powershell script with some function. But I can not figure it out to make it work sequencely. Anyone can help me to arrange it? #Get Date and time $Timestamp = get-date -Format yyyy-mm-dd

#Get the processor architecture
$Get_PA = [environment]::GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")

if($Get_PA = "AMD64"){
$Path = "X:\CUSTOM\ODM\ODMIC\AMD64"}
else
{$Path = "X:\CUSTOM\ODM\ODMIC\AMD64\X86"}

#Get Information from uber.ini
$Get_MAC         = & $Path\UIni.exe X:\uber.ini UnitInformation MACAddress FE_MAC FE_MAC.CMD
start-sleep -s 1
$Read_MAC        = Get-Content .\FE_MAC.CMD
$a_MAC,$b_MAC    = $Read_MAC -split "="

$Get_SN          = & $Path\UIni.exe X:\uber.ini UnitInformation SerialNumber FE_SN FE_SN.CMD
start-sleep -s 1
$Read_SN         = Get-Content .\FE_SN.CMD
$a_SN,$b_SN       = $Read_SN -split "="

$Get_IP       = & $Path\UIni.exe X:\uber.ini FILESERVER0 IP FE_IP FE_IP.CMD
start-sleep -s 1
$Read_IP         = Get-Content .\FE_IP.CMD
$a_IP,$b_IP = $Read_IP -split "="

$Get_netuser  = & $Path\UIni.exe X:\uber.ini FILESERVER0 netuser FE_netuser FE_netuser.CMD
start-sleep -s 1
$Read_netuser = Get-Content .\FE_netuser.CMD
$a_netuser,$b_netuser = $Read_netuser -split "="

$Get_netpass  = & $Path\UIni.exe X:\uber.ini FILESERVER0 netpass FE_netpass FE_netpass.CMD
start-sleep -s 1
$Read_netpass  = Get-Content .\FE_netpass.CMD
$a_netpass,$b_netpass = $Read_netpass -split "="

$Get_UBStage  = & $Path\UIni.exe X:\uber.ini State downloadstage FE_UBStage FE_UBStage.CMD
start-sleep -s 1
$Read_UBStage   = Get-Content .\FE_UBStage.CMD
$a_UBStage,$b_UBStage = $Read_UBStage -split "="


#-----CNT_PRISM-----#

#Connect to PRISM
$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("P:", "\\$b_IP\TempRepository\BPSAutoGM", $False, "$b_netuser", "$b_netpass")
start-sleep -s 5

#Create Working Folder if not exist
$WKFD = "$b_SN-$b_MAC"
$WKFD_Path = "P:\Logs\$WKFD"
if (![System.IO.Directory]::Exists($WKFD_Path ))
{
     New-Item -ItemType Directory -Force -Path $WKFD_Path
}

$FE_ODMIC_WKFD = $WKFD_Path


#Backup Uber.ini
Copy-Item -Path X:\uber.ini -Destination "$FE_ODMIC_WKFD\$b_SN-$Timestamp-$b_UBStage-ODMIC_UBER.INI"


#-----CHK_TOAST-----#
#-----TOAST-----#
Function TOAST
{
#Delete Toast Flag
if(Test-Path -Path "$FE_ODMIC_WKFD\$b_SN-Toast.flg"){
    Remove-Item "$FE_ODMIC_WKFD\$b_SN-Toast.flg"
}

#Copy logs ot working folder
Copy-Item -Path .\BPSFE_ODMIC.log -Destination "$FE_ODMIC_WKFD\$b_SN-$Timestamp-ODMIC_Toast_FEWDTTST.log"

#Toast
$Path_Toast = "X:\Tools\WinToast\"
$Run_Toast         = & $Path_Toast\WinToast.exe /U /PHSA /NA
start-sleep -s 3

#Disconnect to PRISM
& net use P: /delete /yes
start-sleep -s 3
& net use Q: /delete /yes
start-sleep -s 3

#Reboot
Restart-Computer

}

#Check for Toast flag
if(Test-Path -Path "$FE_ODMIC_WKFD\$b_SN-Toast.flg")
{
    #GOTO TOAST#
    TOAST
}
else
{
"Toast flag not found"
}
#-------------------------------------------------------------#

#Auto GM Selection
Function Call_GUI
{
#Run GUI to choose AutoGM
& .\run_gui_1.cmd
start-Sleep -s 1 
$Log = Get-Content .\log.txt | Where-Object {$_.Contains("600")}

if(
    ($Log) -and 
    (![System.IO.Directory]::Exists("$FE_ODMIC_WKFD\AUTOGM")) -and 
    (![System.IO.Directory]::Exists("$FE_ODMIC_WKFD\AUTOGM\JOB")) -and
    (![System.IO.Directory]::Exists("$FE_ODMIC_WKFD\AUTOGM\POfile")) -and
    (![System.IO.Directory]::Exists("$FE_ODMIC_WKFD\AUTOGM\AutoGM.flg"))
){
        New-Item -ItemType Directory -Force -Path "$FE_ODMIC_WKFD\AUTOGM"
        New-Item -ItemType Directory -Force -Path "$FE_ODMIC_WKFD\AUTOGM\JOB"
        New-Item -ItemType Directory -Force -Path "$FE_ODMIC_WKFD\AUTOGM\POfile"
        New-Item -ItemType File -Force -Path "$FE_ODMIC_WKFD\AUTOGM\AutoGM.flg"

    CHK_GM_STAGE

} 
else 
{
    END_Proc
}
}


#-----AUTOGM_SELECT-----#
Function AUTOGM_SELECT
{
#Manually GM or Auto GM selection GUI
if(Test-Path -Path "$FE_ODMIC_WKFD\AUTOGM\AutoGM.flg")
{
    CHK_GM_STAGE
}

else
{
    Call_GUI
}
}

#-----GET_DASH_MODE-----#
#Get DASH Mode
$Get_SKU = & $Path\BiosConfigUtility.exe /getvalue:"SKU Number" | Where-Object {$_.Contains("@ODM@GM")}
if($Get_SKU)
{
$MLGM2ODM = "1"
    AUTOGM_SELECT
}
else
{
    END_Proc
}

Function CHK_GM_STAGE
{
#AUTO GM 

if($MLGM2ODM -eq "1" -and $b_UBStage -eq "101")
{ 
    GM_BEGIN
}

if($MLGM2ODM -eq "1" -and $b_UBStage -eq "444")
{ 
    GM_END
}

else
{
    END_Proc
}
}

#-----GM_BEGIN-----#
Function GM_BEGIN
{
& .\2_GUI_Default_Folder.ps1
}
#-----CHK_GM_STAGE-----#


#-----GM_END-----#
Function GM_END
{

}

#-----END_PROCESS-----#
Function END_Proc
{
#Copy logs ot working folder

#Disconnect to PRISM
& net use P: /delete /yes
start-sleep -s 3
& net use Q: /delete /yes
start-sleep -s 3

}

I tried this code, but it does not work sequencely, so some function do not work. How do I arrange it well, help me please. Thank you.

Bhargav Rao
  • 45,811
  • 27
  • 120
  • 136
Job
  • 375
  • 1
  • 8
  • 27
  • 1
    what is `UIni.exe`? also, why are you getting the info [ip, mac, etc.] from that utility instead of the usual CIM/WMI cmdlets? – Lee_Dailey Jun 17 '19 at 12:20
  • Its complicated to explain it. My question just want to arrange the function correctly – Job Jun 17 '19 at 12:24
  • 1
    There is quite a lot of code here. Most of which has little to do with display / arranging the data. Can you show an example of how it currently looks and what you want it to look like? there is several issues here but first thing I noticed is `![System.IO.Directory]::Exists("$FE_ODMIC_WKFD\AUTOGM\AutoGM.flg")` you create the file if it does not exist however the test is if its a directory. – Matt Jun 17 '19 at 12:33
  • 1
    Also your `if` command is not a comparison but an assignment, as the assignment isn't empty it will ***always*** return true and thus `$path` will always be `"X:\CUSTOM\ODM\ODMIC\AMD64"`. Read [about_comparison_operators](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-5.1) –  Jun 17 '19 at 12:44
  • 1
    @Job - if what you want is to run the steps in sequence, take a look at this ... How to tell PowerShell to wait for each command to end before starting the next? - Stack Overflow — https://stackoverflow.com/questions/1741490/how-to-tell-powershell-to-wait-for-each-command-to-end-before-starting-the-next – Lee_Dailey Jun 17 '19 at 13:00
  • Please don't vandalize your posts. Once you post a question, they belong to the site and its users. Even if it is no longer useful to you, it might still be beneficial to someone in the future. If there is any confidential information revealed in your post, please edit it out with dummy data, and flag for the history to be redacted. – Bhargav Rao Jun 18 '19 at 08:23

2 Answers2

1

As Lee_Dailey suggested [grin], use Start-Process -Wait to run the external programs:

Start-Process -FilePath "<exe>" -ArgumentList @(arg1,arg2) -NoNewWindow -Wait
mhu
  • 17,439
  • 10
  • 60
  • 87
1

Are you saying UIni runs in the background? That would be strange if it outputs text. Piping it to Write-Output might be the less painful way to go, to make powershell wait. And if you used the full path to UIni, you wouldn't need '&'.

js2010
  • 17,785
  • 4
  • 45
  • 50