How to use variable in Powershell?











up vote
0
down vote

favorite












Now is the time to show no mercy for this question but i think this may be the best way to me to learn.
I want to do a for each in one small script i have been working after work.
I want to monitor if any sessions are idle and if are superior to 50 minutes, i want to logoff.
So i use this



(quser)



PS C:Windowssystem32> (quser)
USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME




John rdp-tcp#1 9 Active . 21-11-2018 22:15
Ryan rdp-tcp#0 11 Active 5:07 21-11-2018 10:59




If i use this scrip, i can capture the IDLE Time and compare, this is working fine



**$separator = "," # you can put many separator like this "; : ,"
$partas = (quser) -replace 's{2,}', ','
$parts = $partas.split($separator)
$data = $parts[10]
if ($data -eq "." )
{ $data = "0"
}
$datanow = [int]$data
$toto2 = [int]"45"
if ([int]$datanow -gt [int]$toto2 ){
logoff $parts[6]
Write-Output "Terminator Mode GOD "}
else
{
( Write-Output "Not my president!"
)
}**


As you can see, i splite the parts and select the one i want



**$separator = "," # you can put many separator like this "; : ,"
$partas = (quser) -replace 's{2,}', ','
$parts = $partas.split($separator)
$data = $parts[10]**


But is one session is local, the $parts[x] does not match the real camps and the script stop working.



So i need to make a simpler code, to select the IDLE TIME and the ID to logoff if condition is match.
Can someone help me with this ?



Thanks in advance!c










share|improve this question






















  • Would you consider using WIM/CIM instead of quser.exe? If so, looking over this existing script might help: gallery.technet.microsoft.com/scriptcenter/… If that doesn't help you, take a look at this blog post attempting to solve a similar opportunity. sysadminasaservice.wordpress.com/2015/08/10/…
    – bcdady
    Nov 21 at 23:34










  • you can use the script linked after this to get the output of that exe into a PoSh object. then use those properties to do your select. ///// [PowerShell] function Get-QUserInfo - Pastebin.com — pastebin.com/pe9rkP60
    – Lee_Dailey
    Nov 21 at 23:50










  • @Lee_Dailey gotta work on your markdown: [text](uri)
    – TheIncorrigible1
    Nov 22 at 0:03












  • Thans a lot, I will give a try and return the output after a try.
    – Bruno Baleizao
    Nov 22 at 0:24










  • @TheIncorrigible1 - i don't like hiding the URL, so as long as it aint required, i won't use address hiding. [grin]
    – Lee_Dailey
    Nov 22 at 0:26















up vote
0
down vote

favorite












Now is the time to show no mercy for this question but i think this may be the best way to me to learn.
I want to do a for each in one small script i have been working after work.
I want to monitor if any sessions are idle and if are superior to 50 minutes, i want to logoff.
So i use this



(quser)



PS C:Windowssystem32> (quser)
USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME




John rdp-tcp#1 9 Active . 21-11-2018 22:15
Ryan rdp-tcp#0 11 Active 5:07 21-11-2018 10:59




If i use this scrip, i can capture the IDLE Time and compare, this is working fine



**$separator = "," # you can put many separator like this "; : ,"
$partas = (quser) -replace 's{2,}', ','
$parts = $partas.split($separator)
$data = $parts[10]
if ($data -eq "." )
{ $data = "0"
}
$datanow = [int]$data
$toto2 = [int]"45"
if ([int]$datanow -gt [int]$toto2 ){
logoff $parts[6]
Write-Output "Terminator Mode GOD "}
else
{
( Write-Output "Not my president!"
)
}**


As you can see, i splite the parts and select the one i want



**$separator = "," # you can put many separator like this "; : ,"
$partas = (quser) -replace 's{2,}', ','
$parts = $partas.split($separator)
$data = $parts[10]**


But is one session is local, the $parts[x] does not match the real camps and the script stop working.



So i need to make a simpler code, to select the IDLE TIME and the ID to logoff if condition is match.
Can someone help me with this ?



Thanks in advance!c










share|improve this question






















  • Would you consider using WIM/CIM instead of quser.exe? If so, looking over this existing script might help: gallery.technet.microsoft.com/scriptcenter/… If that doesn't help you, take a look at this blog post attempting to solve a similar opportunity. sysadminasaservice.wordpress.com/2015/08/10/…
    – bcdady
    Nov 21 at 23:34










  • you can use the script linked after this to get the output of that exe into a PoSh object. then use those properties to do your select. ///// [PowerShell] function Get-QUserInfo - Pastebin.com — pastebin.com/pe9rkP60
    – Lee_Dailey
    Nov 21 at 23:50










  • @Lee_Dailey gotta work on your markdown: [text](uri)
    – TheIncorrigible1
    Nov 22 at 0:03












  • Thans a lot, I will give a try and return the output after a try.
    – Bruno Baleizao
    Nov 22 at 0:24










  • @TheIncorrigible1 - i don't like hiding the URL, so as long as it aint required, i won't use address hiding. [grin]
    – Lee_Dailey
    Nov 22 at 0:26













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Now is the time to show no mercy for this question but i think this may be the best way to me to learn.
I want to do a for each in one small script i have been working after work.
I want to monitor if any sessions are idle and if are superior to 50 minutes, i want to logoff.
So i use this



(quser)



PS C:Windowssystem32> (quser)
USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME




John rdp-tcp#1 9 Active . 21-11-2018 22:15
Ryan rdp-tcp#0 11 Active 5:07 21-11-2018 10:59




If i use this scrip, i can capture the IDLE Time and compare, this is working fine



**$separator = "," # you can put many separator like this "; : ,"
$partas = (quser) -replace 's{2,}', ','
$parts = $partas.split($separator)
$data = $parts[10]
if ($data -eq "." )
{ $data = "0"
}
$datanow = [int]$data
$toto2 = [int]"45"
if ([int]$datanow -gt [int]$toto2 ){
logoff $parts[6]
Write-Output "Terminator Mode GOD "}
else
{
( Write-Output "Not my president!"
)
}**


As you can see, i splite the parts and select the one i want



**$separator = "," # you can put many separator like this "; : ,"
$partas = (quser) -replace 's{2,}', ','
$parts = $partas.split($separator)
$data = $parts[10]**


But is one session is local, the $parts[x] does not match the real camps and the script stop working.



So i need to make a simpler code, to select the IDLE TIME and the ID to logoff if condition is match.
Can someone help me with this ?



Thanks in advance!c










share|improve this question













Now is the time to show no mercy for this question but i think this may be the best way to me to learn.
I want to do a for each in one small script i have been working after work.
I want to monitor if any sessions are idle and if are superior to 50 minutes, i want to logoff.
So i use this



(quser)



PS C:Windowssystem32> (quser)
USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME




John rdp-tcp#1 9 Active . 21-11-2018 22:15
Ryan rdp-tcp#0 11 Active 5:07 21-11-2018 10:59




If i use this scrip, i can capture the IDLE Time and compare, this is working fine



**$separator = "," # you can put many separator like this "; : ,"
$partas = (quser) -replace 's{2,}', ','
$parts = $partas.split($separator)
$data = $parts[10]
if ($data -eq "." )
{ $data = "0"
}
$datanow = [int]$data
$toto2 = [int]"45"
if ([int]$datanow -gt [int]$toto2 ){
logoff $parts[6]
Write-Output "Terminator Mode GOD "}
else
{
( Write-Output "Not my president!"
)
}**


As you can see, i splite the parts and select the one i want



**$separator = "," # you can put many separator like this "; : ,"
$partas = (quser) -replace 's{2,}', ','
$parts = $partas.split($separator)
$data = $parts[10]**


But is one session is local, the $parts[x] does not match the real camps and the script stop working.



So i need to make a simpler code, to select the IDLE TIME and the ID to logoff if condition is match.
Can someone help me with this ?



Thanks in advance!c







powershell variables session select






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 23:01









Bruno Baleizao

1




1












  • Would you consider using WIM/CIM instead of quser.exe? If so, looking over this existing script might help: gallery.technet.microsoft.com/scriptcenter/… If that doesn't help you, take a look at this blog post attempting to solve a similar opportunity. sysadminasaservice.wordpress.com/2015/08/10/…
    – bcdady
    Nov 21 at 23:34










  • you can use the script linked after this to get the output of that exe into a PoSh object. then use those properties to do your select. ///// [PowerShell] function Get-QUserInfo - Pastebin.com — pastebin.com/pe9rkP60
    – Lee_Dailey
    Nov 21 at 23:50










  • @Lee_Dailey gotta work on your markdown: [text](uri)
    – TheIncorrigible1
    Nov 22 at 0:03












  • Thans a lot, I will give a try and return the output after a try.
    – Bruno Baleizao
    Nov 22 at 0:24










  • @TheIncorrigible1 - i don't like hiding the URL, so as long as it aint required, i won't use address hiding. [grin]
    – Lee_Dailey
    Nov 22 at 0:26


















  • Would you consider using WIM/CIM instead of quser.exe? If so, looking over this existing script might help: gallery.technet.microsoft.com/scriptcenter/… If that doesn't help you, take a look at this blog post attempting to solve a similar opportunity. sysadminasaservice.wordpress.com/2015/08/10/…
    – bcdady
    Nov 21 at 23:34










  • you can use the script linked after this to get the output of that exe into a PoSh object. then use those properties to do your select. ///// [PowerShell] function Get-QUserInfo - Pastebin.com — pastebin.com/pe9rkP60
    – Lee_Dailey
    Nov 21 at 23:50










  • @Lee_Dailey gotta work on your markdown: [text](uri)
    – TheIncorrigible1
    Nov 22 at 0:03












  • Thans a lot, I will give a try and return the output after a try.
    – Bruno Baleizao
    Nov 22 at 0:24










  • @TheIncorrigible1 - i don't like hiding the URL, so as long as it aint required, i won't use address hiding. [grin]
    – Lee_Dailey
    Nov 22 at 0:26
















Would you consider using WIM/CIM instead of quser.exe? If so, looking over this existing script might help: gallery.technet.microsoft.com/scriptcenter/… If that doesn't help you, take a look at this blog post attempting to solve a similar opportunity. sysadminasaservice.wordpress.com/2015/08/10/…
– bcdady
Nov 21 at 23:34




Would you consider using WIM/CIM instead of quser.exe? If so, looking over this existing script might help: gallery.technet.microsoft.com/scriptcenter/… If that doesn't help you, take a look at this blog post attempting to solve a similar opportunity. sysadminasaservice.wordpress.com/2015/08/10/…
– bcdady
Nov 21 at 23:34












you can use the script linked after this to get the output of that exe into a PoSh object. then use those properties to do your select. ///// [PowerShell] function Get-QUserInfo - Pastebin.com — pastebin.com/pe9rkP60
– Lee_Dailey
Nov 21 at 23:50




you can use the script linked after this to get the output of that exe into a PoSh object. then use those properties to do your select. ///// [PowerShell] function Get-QUserInfo - Pastebin.com — pastebin.com/pe9rkP60
– Lee_Dailey
Nov 21 at 23:50












@Lee_Dailey gotta work on your markdown: [text](uri)
– TheIncorrigible1
Nov 22 at 0:03






@Lee_Dailey gotta work on your markdown: [text](uri)
– TheIncorrigible1
Nov 22 at 0:03














Thans a lot, I will give a try and return the output after a try.
– Bruno Baleizao
Nov 22 at 0:24




Thans a lot, I will give a try and return the output after a try.
– Bruno Baleizao
Nov 22 at 0:24












@TheIncorrigible1 - i don't like hiding the URL, so as long as it aint required, i won't use address hiding. [grin]
– Lee_Dailey
Nov 22 at 0:26




@TheIncorrigible1 - i don't like hiding the URL, so as long as it aint required, i won't use address hiding. [grin]
– Lee_Dailey
Nov 22 at 0:26

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421659%2fhow-to-use-variable-in-powershell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421659%2fhow-to-use-variable-in-powershell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...