G
G
genbachae2020-05-13 09:24:27
PowerShell
genbachae, 2020-05-13 09:24:27

How to correctly assign a string in Power Shell?

When executing the code:

$watch = [System.Diagnostics.Stopwatch]::StartNew()
$watch.Start()                                        #Запуск таймера
Start-Sleep 1
$watch.Stop()                                         #Остановка таймера
$s = "Время выполнения: "
$t = Write-Host $watch.Elapsed                        #Время выполнения
$s = $s + $t.ToString()


An error pops up: "Unable to call a method on an expression with a null value."

Can you please tell me how to assign a string so that the error does not pop up?
5ebb9299f00e9561839910.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Bezrukov, 2020-05-13
@genbachae

$watch = [System.Diagnostics.Stopwatch]::StartNew()
$watch.Start() #Start timer
Start-Sleep 1
$watch.Stop() #Stop timer
Write-Host "Runtime:"$watch.Elapsed

A
azarij, 2020-05-13
@azarij

$watch = [System.Diagnostics.Stopwatch]::StartNew()
$watch.Start()                                        #Запуск таймера
Start-Sleep 1
$watch.Stop()                                         #Остановка таймера
$s = "Время выполнения: "
Write-Host $watch.Elapsed
$t = $watch.Elapsed                     #Время выполнения
$s = $s + $t.ToString()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question