2017年6月21日 星期三

Delphi 判斷字串是否為中文字

可以透過 ByteType函式來判斷字串的內容,藉以辨識每個字節的型態。

function ByteType(const S: AnsiString; Index: Integer): TMbcsByteType;
function ByteType(const S: UnicodeString; Index: Integer): TMbcsByteType;

回傳內容:
mbSingleByte : 單字節字串
mbLeadByte   : 雙字節(中文字)字串的第一個字符
mbTrailByte  : 雙字節(中文字)字串的第二個字符

範例:
var
  Str, S1,S2 : String ;    // AnsiString
  I : integer ;
begin
  Str := '12判ew斷dd某一ccc個111' ;
  S1 := '' ;
  S2 := '' ;
  for I := 1 to Length(Str) do
  begin
    if ByteType(Str,I) = mbSingleByte then
      S1 := S1 + Str[I]
    else
    if ByteType(Str,I) in [mbLeadByte,mbTrailByte] then
      S2 := S2 + Str[I] ;

  end;
  // 輸出結果
  S1 -->  12ewddccc111
  S2 -->  判斷某一個
end ;

2017年2月7日 星期二

PowerShell 將主機設定允許遠端管理

PowerShell 將主機設定允許遠端管理

要使用PowerShell進行遠端主機管理時,被管理端必須要允許可以進行遠端管理,可以參考下列網址說明:
https://technet.microsoft.com/en-us/library/ff700227.aspx

節錄該網站部分內容如下:

You can verify the availability of WinRM and configure a PowerShell for remoting by following these steps: 
1. Start Windows PowerShell as an administrator by right-clicking the Windows PowerShell shortcut and selecting Run As Administrator. 

2. The WinRM service is confi gured for manual startup by default. You must change the startup type to Automatic and start the service on each computer you want to work with. At the PowerShell prompt, you can verify that the WinRM service is running using the following command: 
get-service winrm
The value of the Status property in the output should be “Running”.

3. To configure Windows PowerShell for remoting, type the following command: 
Enable-PSRemoting –force

In many cases, you will be able to work with remote computers in other domains. However, if the remote computer is not in a trusted domain, the remote computer might not be able to authenticate your credentials. To enable authentication, you need to add the remote computer to the list of trusted hosts for the local computer in WinRM. To do so, type: 
winrm s winrm/config/client '@{TrustedHosts="RemoteComputer"}'
Here, RemoteComputer should be the name of the remote computer, such as: 
winrm s winrm/config/client '@{TrustedHosts="CorpServer56"}'


另外,若是執行上述程序後,遠端連結到主機端,還是會出現 Access is denied (拒絕存取),很有可能是UAC的因素,依據網路上找到的說明,若是主機有啟用UAC,遠端連線控制所使用的帳戶,若是網域帳戶,且在Administrators群組,則不受影響,但若使用的帳戶是屬於LocalAccount,則UAC不允許遠端訪問WinRM服務,若需要使用,則需要建Registry中建置下列值DWORd設定為1,以允許LocalAccount執行WinRM服務

[HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System] LocalAccountTokenFilterPolicy

或者透過系統管理員身分執行CMD,輸入下列指令新增Registry參數:

reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 1 /f" and then "winrm quickconfig


2016年10月18日 星期二

連線遠端主機-PowerShell

透過PowerShell可以連線到遠端主機進行維護,要連線到遠方主機,除了遠端主機要允許執行外,連線時所使用的帳號也不需要再Administrators群組內才行。
若要連線至遠端主機時,必須要先進行認證,一般可以採取下列做法來執行。

1. 先建立認證物件。

$username = "your account"
$password = "your password" | ConvertTo-SecureString -asPlaintext -Force    
$servername = "your ServerName or IP"
$credential = New-Object System.Management.Automation.PSCredential($username, $password)

2. 透過以建立之認證物件,連線至遠端主機進行維護,例如透過WMI的Win32_UserAccount來取得遠端主機帳號清單,如下:

Get-WmiObject -Class Win32_UserAccount -ComputerName $servername -Credential $credential


PowerShell更改網路類型

1. 開啟Powershell,輸入Get-NetConnectionProfile , Enter後,可以看到目前作用中網路的網路類型。


NetworkCategory欄位中便是目前嘎網卡的網路狀態,目前是Publie(公用網路)

2. 若要變更網路類型,可以透過Set-NetConnectionProfile指令來執行,如下範例:

    Set-NetConnectionProfile  -InterfaceIndex 9 -NetworkCategory Private ,Enter後,便將該網路類型更改為(私人網路)。

    注意,InterfaceIndex的參數值,需要參考實際網路類型中顯示的數值。

2016年10月17日 星期一

Redmine建置紀錄-使用Bitnami

Redmine是一套專案管理網站,Bitnami有提供已建置好的VM供直接下載使用,網址如下:

第一次啟動後,登入的帳密皆是bitnami(畫面上有提示),OS是ubuntu Linux,登入後會要求更換帳密。
進入後,先進行設定網卡,修改如下:

sudo nano /etc/network/interfaces    按Enter進入網卡設定畫面

auto eth0
inface eth0 inet static          # 固定(靜態)IP
address xxx.xxx.xxx.xxx      # IP位置
netmask 255.255.255.0      # 網路遮罩
gateway xxx.xxx.xxx.xxx     # 預設閘道
dns-nameserver 8.8.8.8      #DNS Server  有兩個以上,就用空格分開

修改儲存後,重新啟動網卡: (試了好像沒有用)
sudo /etc/init.d/networking restart


Bitnami的VM啟動後,SSH未能自動啟動,進行下列程序即可
$ sudo mv /etc/init/ssh.conf.back /etc/init/ssh.conf
$ sudo start ssh



E-Mail寄送設定
sudo nano /opt/bitnami/apps/redmine/htdocs/config/configuration.yml

裡面原本就有提供範例,修改以下內容(以新陽Mail為例)

production:
  email_delivery:
    delivery_method: :smtp

    smtp_settings:
      enable_starttls_auto: true
      address: "mail.thinkon.com.tw"
      port: '25'
      domain: "smtp.gmail.com"
      authentication: :plain
      #因為透過內部發信,就沒有設定帳密
      #user_name: "your_email@gmail.com"

      #password: "your_password"

設定後,需要將其他範例Mark或刪除掉,不然會出錯


LDAP設定
Redmine若要讓帳號透過公司的AD進行認證,則要設定其LDAP,設定畫面如下:



其中[登入屬性]欄位內容依定要設定為[sAMAccountName]才行


資料庫備份:
資料庫帳密,請參考 database.yml中的設定

備份指令
mysqldump -u<帳號> -p<密碼> <資料庫名稱> > 備份檔名稱


Ex: mysqldump -ubitnami -p bitname_redmine > redmine_db.sql