Scheduled powershell task issue

Scheduled powershell task issue

avatar
kstonernasg
Disabled

I have noticed that when my powershell script to update or add new teamviewer sessions will always add a new session to the database, os there a way around it?

Manually kicking off the scrip works normally, however when it runs as a scheduled task it's creating new sessions with the same information.

Let me know if I can provide more information. Using the current Beta 9.4.9.0

All Comments (15)

avatar

Can you send me your script so that I can have a look at it. (via private message if you like)

Stéfane Lavergne

avatar

CMD script:
start /wait %SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File UpdateRDM.ps1
exit
PS Script:
#********************************************
#* Script to update Teamvier session names *
#* Written by Ken Stoner *
# Updated 07-15-2014 14:39 *
#********************************************
Get-PSSnapin -Name *Remote.Desktop* -Registered | % { Write-Host "$_..." -NoNewline; if ( (Get-PSSnapin -Name $_ -ErrorAction SilentlyContinue) -eq $null ) { Write-Host "installing" -ForegroundColor Yellow; Add-PsSnapin $_ } else { Write-Host "already installed" -ForegroundColor Green; } }
$ServerName = "DBSERVER"
$DatabaseName = "Teamviewer"
$Query = "Select distinct rtrim(ltrim(TVId)) as TVID, CombinedName, rtrim(ltrim(Location)) as Location from (SELECT *,MaxDate= Max([LastUpdate])OVER (PARTITION BY TVId)FROM [Teamviewer].[dbo].[TeamViewerID]) as s where LastUpdate = MaxDate and len(TVId) <>0"
#Timeout parameters
$QueryTimeout = 120
$ConnectionTimeout = 30
#Action of connecting to the Database and executing the query and returning results if there were any.
$conn=New-Object System.Data.SqlClient.SQLConnection
$ConnectionString = "Server={0};Database={1};Integrated Security=True;Connect Timeout={2}" -f $ServerName,$DatabaseName,$ConnectionTimeout
$conn.ConnectionString=$ConnectionString
$conn.Open()
$cmd=New-Object system.Data.SqlClient.SqlCommand($Query,$conn)
$cmd.CommandTimeout=$QueryTimeout
$ds=New-Object system.Data.DataSet
$da=New-Object system.Data.SqlClient.SqlDataAdapter($cmd)
[void]$da.fill($ds)
$conn.Close()
$dataTable = $ds.Tables[0]
#Get existing sessions "Teamviwer"
$sessions = Get-RDM-Session | where {$_.Session.Kind -eq "TeamViewer"}
#Get the Teamviwer ID inside from the session
$TVIDs = $sessions.Session.GetProperty("TeamViewer", "ID")
$UpdateCount = 0
$AddCount = 0
#Update the Sessions or add them
$dataTable | FOREACH-OBJECT {
Write-Host " "
$HostTVId= $_.TVId
$HostName= $_.CombinedName
$HostLocation = $_.Location + "\TeamViewer"
Write-Host "Returned From SQL (HostName: "$HostName " TeamViewerID: " $HostTVId " Location: " $HostLocation -foreground "Magenta"

if ($TVIDs -contains $HostTVId)
{
#Update session name
Write-Host "Updating:" $HostName -foreground "Yellow"
$UpdateSession = Get-RDM-Session |where {$_.Session.getProperty("TeamViewer", "ID") -eq $HostTVId}
$UpdateSession[0].Session.Name = $HostName
$UpdateSession[0].Session.Group = $HostLocation
Set-RDM-Session $UpdateSession[0].Session -NoRefresh;
$UpdateCount = $UpdateCount + 1
}
else
{
#Add Session name
Write-Host "Adding:" $HostName
$session = New-RDM-Session -Name $HostName -Kind "TeamViewer"
$session.Group = $HostLocation
$session.TeamViewer.ID = $HostTVId;
Set-RDM-Session $session -NoRefresh;
$pass = ConvertTo-SecureString "PWHere" -asplaintext -force;
Set-RDM-Password -ID $session.ID -Password $pass -NoRefresh;
$AddCount = $AddCount + 1
}

}
Write-Host " "
Write-Host "Sessions Updated:" $UpdateCount -foreground "White"
Write-Host "Sessions Added:" $AddCount -foreground "White"
Start-Sleep -s 10


Seems everytime the scheduler kicks it off a new session is created. However if I run the cmd file from a prompt it works fine every time. I've tried using an admin account and a system account to run the task.

avatar

It appears to be the user account that powershell is launched with, If I use my account to run: Get-RDM-Session | where {$_.Session.Kind -eq "TeamViewer"} I get all the TV accounts. If I run it with a different account (admin) I get either no or different results.

Anyway to fix this? I would like to update the record no matter who put it in.

avatar

Does it create the exact same "new session" each and every time it runs?

Could you please post the "new session" that gets created. (right-click -> Clipboard -> Copy) and past the XML code here (just like you did the script). Make sure to remove any/all confidential information.

Regards,

Stéfane Lavergne

avatar

Just saw your other post, we replied at the same time :-)

Stéfane Lavergne

avatar

Are you using Security Groups in RDM?

Stéfane Lavergne

avatar

Will run it again and post the output. I currently do not have any security groups.

avatar

first run:
<?xml version="1.0"?>
<ArrayOfConnection>
<Connection>
<ActiveDirectoryConsole />
<ConnectionType>TeamViewer</ConnectionType>
<CreatedBy>Domain\kstoner</CreatedBy>
<CreationDateTime>2014-07-16T14:32:00.1112706-05:00</CreationDateTime>
<Events />
<ID>c9291477-ce0d-4acc-a14c-9ff58ee7d238</ID>
<MetaInformation>
<PasswordHistory>
<PasswordHistory>
<ModifiedBy>Domain\kstoner</ModifiedBy>
<ModifiedDateTime>2014-07-16T14:32:02.1424426-05:00</ModifiedDateTime>
<SafePassword>scrubbed==</SafePassword>
</PasswordHistory>
</PasswordHistory>
</MetaInformation>
<Name>Stewart, Stacey - Domain-1362</Name>
<OpenEmbedded>true</OpenEmbedded>
<Stamp>a122905a-f929-462c-9b14-17cce222bae4</Stamp>
<TeamViewer>
<ID>12345678</ID>
<SafePassword>scrubbed==</SafePassword>
</TeamViewer>
<UpdateDateTime>2014-07-16T14:32:02.1424426-05:00</UpdateDateTime>
<UpdatedBy>Domain\kstoner</UpdatedBy>
</Connection>
</ArrayOfConnection>
after 2nd run:


<?xml version="1.0"?>
<ArrayOfConnection>
<Connection>
<ActiveDirectoryConsole />
<ConnectionType>TeamViewer</ConnectionType>
<CreatedBy>Domain\kstoner</CreatedBy>
<CreationDateTime>2014-07-16T14:39:34.9844278-05:00</CreationDateTime>
<Events />
<ID>aa452206-4d5a-42b8-bb9a-cd098cc65aa4</ID>
<MetaInformation>
<PasswordHistory>
<PasswordHistory>
<ModifiedBy>Domain\kstoner</ModifiedBy>
<ModifiedDateTime>2014-07-16T14:39:36.8593558-05:00</ModifiedDateTime>
<SafePassword>scrubbed==</SafePassword>
</PasswordHistory>
</PasswordHistory>
</MetaInformation>
<Name>Stewart, Stacey - Domain-1362</Name>
<OpenEmbedded>true</OpenEmbedded>
<Stamp>b4029959-c77d-47bf-99ac-6330805b76b3</Stamp>
<TeamViewer>
<ID>12345678</ID>
<SafePassword>scrubbed==</SafePassword>
</TeamViewer>
<UpdateDateTime>2014-07-16T14:39:36.8593558-05:00</UpdateDateTime>
<UpdatedBy>Domain\kstoner</UpdatedBy>
</Connection>
</ArrayOfConnection>
note scrubbed PW and changed teamviewer ID, the id was identical between them.

avatar

something I just noticed, when I right click>clipboard>copy the session disappears.

avatar

I can't reproduce the issue on my side. Could you try removing the "-NoRefresh" to see if that changes anything?

Stéfane Lavergne

avatar

Removing norefresh didn't seem to make a bit of difference. Is it somehow related to the way it's scheduled? I can' understand why when I run the script in a loop it works fine until I schedule it to run once an hour.
edited by kstonernasg on 7/18/2014

avatar

Could you try the latest beta build.

http://remotedesktopmanager.com/Home/Download#beta

We've fixed a breaking change from a few versions ago that could be the root cause of your issue.

Regards,

Stéfane Lavergne

avatar

Finally had a chance to test this, and so far so good the updated version appears to work. I'll keep an eye on it today and confirm.

avatar

Good to here. Thank you for the feedback.

Regards,

Stéfane Lavergne

avatar

Confirmed resolved!