Put a Powershell Ouput into a variable inside a batch script [duplicate] - powershell

This question already has answers here:
How to set commands output as a variable in a batch file [duplicate]
(9 answers)
Closed 2 years ago.
I'm making following batch script
set FileVersion = powershell (Get-Command C:\Tool.exe).FileVersionInfo.ProductVersion
echo %FileVersion%
I'd like retrieve attribute 'Product Version' of file C:\Tool.exe. To do that I use a powershell command and I want save that value to a variable called 'File Version' and later print out it, but it doesnt works!
Can you help me?

For some yet to be understood logic, a FOR loop must be used.
FOR /F "delims=" %%A IN ('powershell (Get-Command C:\Tool.exe).FileVersionInfo.ProductVersion') DO (SET "FileVersion=%%~A")

Related

Can you please help me with a powershell script to ping a list of servers from a selected source [duplicate]

This question already has answers here:
Ping a list of host names and output the results to a csv in powershell
(4 answers)
Import-CSV to array, then run through a ForEach loop to ping each IP
(1 answer)
PING results to CSV
(2 answers)
Closed 3 months ago.
Can you please help me with a PowerShell script to ping a list of servers from a selected source and provide the output of failed and successful in CSV format
Can you please help me with a PowerShell script to ping a list of servers from a selected source and provide the output of failed and successful in CSV format?
tried the below batch script but a blank ping.txt came up.
#echo off
type NUL > ping.txt
FOR /F "tokens=1 delims= " %%D IN (import.csv) DO (
FOR /F "tokens=3 delims= " %%S IN (import.csv) DO (
ping `%%D` -l 0 -S `%%S` >> ping.txt
))

How to execute a powershell script using a batch file? [duplicate]

This question already has answers here:
Why is no string output with 'echo %var%' after using 'set var = text' command in cmd? [duplicate]
(2 answers)
Closed 3 years ago.
I have powershell script which I am trying to execute using a batch file. I am not sure how to direct the .bat file to the powershell script without hardcoding the full path.
Both the script and .bat files are stored in the same folder. I've got this so far but it doesn't appear to do anything.
#ECHO OFF
REM PowerShell -WindowStyle Hidden -NoProfile....
SET ScriptDirectory = %~dpn0
SET ScriptPath = %ScriptDirectory% || "/" || counterscript.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%ScriptPath%'";
I don't think you can. You can only hard code the path. Maybe there is some way that you can do it, but I don't know any other ways rather than hard coding. You could hard code the file name though.

Is there a way to capture the file path and name in the same batch function?

I am creating a batch script to perform robocopy functions. Currently I am having to call two different PowerShell selections, one for the file name and then one for the source folder, can I combine this?
Using the code below I can capture the file name, but can I capture both using one method?
echo Select your file
set pwshcmd=powershell -NoProfile -Command "&{[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | Out-Null;$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog; $OpenFileDialog.ShowDialog()|Out-Null; $OpenFileDialog.SafeFileName}"
for /f "delims=" %%I in ('%pwshcmd%') do (
set "FileName=%%I"
)
echo %FileName%
pause
I wish for the user to make one selection with PowerShell and it set two variables %FileName% and %FilePath%, as this will be used in the robocopy script.
I appreciate everyone's input on this question. I had solved it shortly after posting, however I did run into the fact that robocopy wants a directory path without the ending "\". The method used to gather both paths were:
for /f "delims=" %%I in ('%pwshcmd%') do (
set "r_copy_file_source_path=%%~dpI"
set "r_copy_file_source=%%~nxI"
)
This provided me with both variables required, then I trimmed the ending "\" from the path with:
set r_copy_file_source_path=%r_copy_file_source_path:~0,-1%
Again, I appreciate the responses! Thank you!

How to pass batch file variables to PowerShell script? [duplicate]

This question already has an answer here:
Displaying SET variable
(1 answer)
Closed 3 years ago.
I am attempting to pass a number of variables processed by my batch file to a Powershell script. The problem I face is that firstly the entire results from the batch file come up in command prompt and next to the variables I intend to pass are not passed to the Powershell Script. Additionally, the variable I have to output the contents of the log file in just send the command back to the screen.
I have tried the following links and these links got me as far as I am now:
Batch file to execute a Powershell script
Pass variable from batch to powershell
Pass parameter from a batch file to a PowerShell script
Pass batch variables with spaces in them to powershell script?
Batch File side
set LOG_FILE = "GDGAGnklasj;oks;fk;dkf lkl;"
set oName = Name
set oStart = "%YYYY%%MM%%DD% %TIME%"
set oStatus = 0
set oEnd = "%YYYY%%MM%%DD% %TIME%"
set oDateRan = %YYYY%%MM%%DD%
set oLog =for /f "delims=" %%i in (%LOG_FILE%) do set content=%content% %%i
echo Updating Database >> %LOG_FILE% 2>&1
cmd /S powershell.exe -ExecutionPolicy Bypass -File "C:\Reporting\updateTool.ps1" "%oName%" "%DateRan%" "%oStart%" "%oEnd%" "%oStatus "%oLog%
PowerShell Script
param (
[string]$oName
)
"This is $oName"
My intent is to set the variables within the batch file then send them to Powershell for processing.
Be very careful of spaces.
set oName=taco
PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "& '.\ScriptName.ps1' -oName '%oName%' "
Oh Easy-Peasy, I do this for my Power shells that we need CMD wrappers for quite a bit.
I have to run to the train so this is going to be a bit meh at the moment I will firm it up in a bit, right now just going to paste in some example code so I can make it your code
Okay, what, umm, what did you intend for this Particular code to.l do ? I can't seem to figure out what you were intending with this, is it just some dummy code?
set oLog =for /f "delims=" %%i in (%LOG_FILE%) do set content=%content% %%i
echo Updating Database >> %LOG_FILE% 2>&1
Okay on further review I think you want to read the log into a couple of sttring variables in CMD, then use one of them in your call of the script..... but, why?
The strings will append to each other and you will be limited to 8191 characters max, and PowerShell can easily read the content of the log file because you pass the name to Powershell.
That seems like a better plan, no?
All your code where you have YYYY MM DD those are variables you will need to define before using, not sure if that is understood if so all good.
.CMD Script:
#(
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO OFF
SET "_PSScript=C:\Reporting\UpdateTool.ps1"
REM SET "_DebugPreference=Continue"
SET "_DebugPreference="SilentlyContinue"
SET "_LOG_FILE=GDGAGnklasj;oks;fk;dkf lkl;"
SET "_oName=Name."
SET "_oStart=%YYYY%%MM%%DD% %TIME: =0%"
SET /a "_Status=0"
SET "_oEnd=%YYYY%%MM%%DD% %TIME: =0%"
SET "_oDateRan=%YYYY%%MM%%DD%"
)
SET "_PSCMD=Powershell "%_PSScript%" -DebugPreference "%_DebugPreference%" -LOG_FILE "%_LOG_FILE%" -oName "%_oName%" -oStart "%_oStart%" -Status %_Status% -oEnd "%_oEnd%" -oDateRan "%_oDateRan%" "
%_PSCMD% 2>&1 >> "_LOG_FILE"
PS1:
## Script: UpdateTool.ps1
#
param(
[String]$LOG_FILE = 'c:\admin\default.log',
[String]$oName = 'default name'
[String]$oStart = $(Get-date -F "yyyyMMdd HH:mm:ss.ms"),
[Int]$oStatus = 0,
[String]$oEnd = $(Get-date -F "yyyyMMdd HH:mm:ss.ms"),
[String]$oDateRan = $(Get-date -F "yyyyMMdd"),
$DebugPreference = "SilentlyContinue"
)

How to insert date and time to a filename in a batch script? [duplicate]

This question already has answers here:
How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?
(30 answers)
Closed 5 years ago.
I wrote a batch script for a Jenkins jobs which compiles a ".net" code and one of the steps there is to back up the current directory before extracting the new compiled code.
I'm using the these lines to extract the date and time which I want to insert into the backup file name:
for /F "tokens=2-4 delims=/ " %%i in ('date /t') do set date=%%k%%i%%j
for /f "tokens=1-2 delims=/:" %%a in ('time /t') do set time=%%a%%b
powershell.exe -nologo -noprofile -command "& { Add-Type -A System.IO.Compression.FileSystem; [IO.Compression.ZipFile]::CreateFromDirectory('bin', 'bin-%date%_%time%.zip'); }"
xcopy bin-%date%_%time%.zip c:\temp
The problem is with the output of time /t which looks like that:
01:00 AM
And that causes the filename to be:
bin-20170412-0100 AM.zip
which is a problem.
I want to omit the " AM" from the time variable, how can it be done?
Because you obviously have no issue with powershell usage, replace those first two lines with:
For /F %%A In ('Powershell -C "Get-Date -Format yyyyMMdd_HHmm"'
) Do Set "archive=bin-%%A.zip"
You then have your full archive file name set in the variable %archive% for use in your following commands.